home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / python / mail1992 < prev    next >
Encoding:
Text File  |  1994-04-01  |  967.9 KB  |  25,582 lines  |  [TEXT/R*ch]

  1. 
  2. Received: by charon.cwi.nl with SMTP; Fri, 22 Nov 1991 11:47:14 +0100
  3. Received: by voorn.cwi.nl with SMTP; Fri, 22 Nov 1991 10:47:13 GMT
  4. Message-Id: <9111221047.AA11471@voorn.cwi.nl>
  5. To: python-list@cwi.nl
  6. Subject: Welcome to the Python list!
  7. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  8. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  9. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  10. Date: Fri, 22 Nov 91 11:47:11 +0100
  11. Sender: Guido.van.Rossum@cwi.nl
  12.  
  13. Welcome to the Python mailing list!  This message acknowledges that
  14. you are now on the list.
  15.  
  16. The list is intended for discussion of and questions about all aspects
  17. of design and use of the Python programming language.  The list is not
  18. moderated, i.e., everything you send to it is immediately forwarded to
  19. anybody else on the list -- so be a little careful in what you write.
  20.  
  21. Send messages for the entire list to python-list@cwi.nl.  Send
  22. (un)subscription request and other messages for the list administrator
  23. (that's me) to python-list-request@cwi.nl (that's python-list-request,
  24. not python-request!).
  25.  
  26. A high-level description of Python can be found in section two of the
  27. following paper:
  28.  
  29.     Guido van Rossum, Jelke de Boer: Linking a Stub Generator
  30.     (AIL) to a Prototyping Language (Python); in: Spring 1991
  31.     EurOpen Conference Proceedings, May 20-24, 1991, Troms/o,
  32.     Norway.
  33.  
  34. The latest Python release is always available by anonymous ftp from
  35. ftp.cwi.nl, in directory pub.  Chances are that it is also available
  36. from wuarchive.wustl.edu, which may be faster for U.S. based users.
  37. The release contains full source and documentation, and runs on most
  38. UNIX workstations and on Macintosh computers (who'll do an MS-DOS
  39. port?)
  40.  
  41. --Guido van Rossum, CWI, Amsterdam
  42. "Is it a bird? No! Is it a plane? No! It's bicycle repair man!"
  43. 
  44. 
  45. Received: by charon.cwi.nl with SMTP; Sat, 23 Nov 1991 16:35:14 +0100
  46. Received: by voorn.cwi.nl with SMTP; Sat, 23 Nov 1991 15:35:13 GMT
  47. Message-Id: <9111231535.AA13912@voorn.cwi.nl>
  48. To: python-list@cwi.nl
  49. Subject: Re: comments on Python
  50. Date: Sat, 23 Nov 91 16:35:12 +0100
  51. From: Guido.van.Rossum@cwi.nl
  52.  
  53. [To get some discussion going on the list, I post (with permission) a
  54. reply I sent to a message from one of my beta testers, which discusses
  55. various aspects of Python.  Almost the entire original message is quoted
  56. using ">" in the left margin.]
  57.  
  58. >From: amdcad!bitblks!bvs@uunet.UU.NET
  59. >
  60. >The package:
  61. >    mode_t is not defined for sunos-3
  62. >    waitpid is not defined for sunos-3
  63. >
  64. >    That is, these two do not exist for sunos-3 so fake defns
  65. >    should be provided.
  66.  
  67. I'll try.  Is there some predefined symbol by which I can recognize
  68. sunos-3 from SunOS 4?
  69.  
  70. >    The error message when you forget to indent (as in the
  71. >    example below) is very cryptic.
  72. >        while b<10:
  73. >        print b,
  74. >    Not sure what you can do though.
  75.  
  76. True, all too true.  In fact there is only a single error message for
  77. syntax errors.  Some day I'll put in a decent parser.
  78.  
  79. >    You really should number pages in tut.tex.  A pain to keep
  80. >    them sorted.
  81.  
  82. When I print it the pages are numbered.  Maybe you should shorten the
  83. page height in "myformat.sty" -- I set it for European paper size,
  84. which is about 12 inches high, while yours is likely to be 11 inches.
  85.  
  86. >I like python on the whole and don't like perl.  Perl lacks in
  87. >data structures (one can't have an array of an array or an array
  88. >of dictionaries) and has a very irregular syntax.  Lack of
  89. >explicit declarations means you can misspell a word and not know
  90. >it until the program mis-behaves.  It has dynamic scoping and uses
  91. >lexical hooks like & @ $ etc. to mean different things.
  92.  
  93. Actually, Python lacks explicit declarations as well -- and indeed it
  94. is one of its weaknesses that a misspelled variable is only detected
  95. when the actual line of code is executed (or never, if an *assignment*
  96. is misspelled and there was a previous assignment to the same name).
  97. Clever compile-time checking can relieve this in part, although for
  98. instance variables (assignable attributes in Python's parlance) it
  99. gets tough.
  100.  
  101. >BUT.... the fact remains that perl is still a lot easier to hack
  102. >something in, due to the conciseness of its notation, some very
  103. >powerful operators, multiple features for doing the same thing,
  104. >pattern matching operators, and lots of examples.  If you try to
  105. >translate some existing perl scripts to python, you can see for
  106. >yourself what I am saying.  Or may be, what is needed is lots of
  107. >useful text munging examples.
  108.  
  109. Python on purpose gives you one way to do most things (one way for
  110. each things, not one way for all things together!), on the account
  111. that it makes the choice easier, and makes programs more readable --
  112. you are less likely to encounter unobvious code for obvious actions.
  113. There is a demo directory in the distribution which tries to give the
  114. examples, and part of the library is also intended as such -- but more
  115. examples always help.  Text munging wasn't the first purpose of the
  116. language, although it is an important tool for many applications, just
  117. like arithmetic.
  118.  
  119. >Anyway, here are some suggestions.  I believe python can be made
  120. >significantly friendlier by adding a few choice features but
  121. >without sacrificing its clean and sound design.  May be a lot of
  122. >the things user might want can be added to the library.
  123.  
  124. I will resist creeping featurism wherever I can, but I have indeed
  125. been adding features lately (e.g., shift/mask operators).
  126.  
  127. >How about providing assignment operators?  Some time one has to
  128. >build a thing piece-meal and this sort of operators are very
  129. >handy.  The semantics are clear:
  130. >
  131. >    <var> <op>= <expr>  === <var> = <var> <op> <expr>
  132. >
  133. >Example:
  134. >    a = 'ba '
  135. >    a *= 2                  # a = 'ba ba'
  136. >    a += 'black sheep'      # a = 'ba ba black sheep'
  137.  
  138. Agreed.  Now that the parser can actually recognize two-character
  139. operators, adding these wouldn't be so hard (except that the grammar
  140. tables will grow again :-( ).
  141.  
  142. >I would also like the capability of overloading standard operators
  143. >for predefined and user defined classes.  Thus instead of
  144. >       result.append(b)
  145. >I should be able to say
  146. >    result += b
  147. >or better still,
  148. >    result += [a,b,c]
  149.  
  150. The latter should automatically work if we let x += y be syntactic
  151. sugar for x = x+y.  Note that result = result + [x] has a different
  152. meaning than result.append(x), although in many cases it doesn't
  153. matter: result.append(x) modifies the existing object that result is
  154. bound to, while result = result+[x] creates a new list (the
  155. concatenation of result and [x]) and binds result to it, forgetting
  156. the object to which result was bound previously.  If there was another
  157. reference to that object, e.g., through an assignment "z = result"
  158. earlier on, the value of z is unchanged by the second form, but
  159. modified by the first form.  Confusing?  Yes, but this is a lot better
  160. than allowing arbitrary pointers!
  161.  
  162. >How come python doesn't have structures?  Do I have to use classes
  163. >for this ala C++?  Ugh!
  164.  
  165. For simple cases you can use tuples -- this keeps values together but
  166. you reference them by position.  For larger cases you indeed have to
  167. use classes -- but what's ugly about that?
  168.  
  169. >As in perl I would like to be able to assign elements of an array
  170. >to individual variables or vice-versa.  This is very handy.  For
  171. >example, the following code picks out size and name of all the
  172. >files in the current directory.
  173. >
  174. >    open(LS, "ls -l|") || die "ls failed";
  175. >    for (<LS>) {
  176. >        ($perm,$links,$user,$size,$Mon,$day,$YorT,$name) = split;
  177. >        print "$size\t$name\n";
  178. >    }
  179. >
  180. >Granted that a list is not the same as a tuple so perhaps we
  181. >need a special `coercion' operator or assignment. I am hoping
  182. >for something like
  183. >
  184. >    ls = open('ls -l', '<')
  185. >    if not ls: die('ls failed')
  186. >    while line = ls.readline():
  187. >        perm,links,size,mon,day,YorT,name =~ tuple split(line)
  188. >        print size, '\t', name, '\n'
  189. >
  190. >    Notes:
  191. >    '<' in open implies pipe-from
  192. >    '>' in open should imply pipe-to
  193. >    operator tuple coerces a list to a tuple.
  194. >    operator =~ allows mismatched tuples on the LHS and RHS.
  195.  
  196. You can do this!  If a is a list of three elements, just write
  197.     [x, y, z] = a
  198. A working version of this program is:
  199.  
  200. import posix, string
  201. pipe = posix.popen('ls -l', 'r')
  202. for line in pipe.readlines():
  203.     if line[:5] = 'total': continue
  204.     [perm,links,user,group,size,mon,day,YoT,name] = string.split(line)[:9]
  205.     print size, '\t' + name
  206.  
  207. The [:9] tacked to the end of the assignment does more or less what your
  208. proposed =~ does.  Note that "ls -l" on my system outputs both the
  209. username and the group name, and outputs a first line starting with
  210. 'total', which makes this kind of scripts so dangerously non-portable.
  211. Another way (oh no!) to do this in Python was to use posix.opendir()
  212. and posix.stat(); I'll let that be an exercise for the reader (actually
  213. there are examples of this in the demo subtree).
  214.  
  215. >The support for regular expressions is built in perl.  This is
  216. >very handy.  Perhaps an equivalent capability can be added via a
  217. >library module?  Though that is likely to be very slow (just like
  218. >the operations in string.py).
  219.  
  220. There's already a built-in module 'regexp', which does what you want.
  221. Access is slightly clumsier than Perl (there's no special syntax to
  222. handle regular expressions) and the expressions are simpler (just
  223. Henry Spencer's regexp package), but what you want is there...
  224.  
  225. >Got to go.
  226.  
  227. Me too.
  228.  
  229. >-- Bakul
  230.  
  231. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  232. "All right, it's a fair cop, but society is to blame."
  233. 
  234. 
  235. Received: by charon.cwi.nl with SMTP; Sat, 23 Nov 1991 16:38:40 +0100
  236. Received: by voorn.cwi.nl with SMTP; Sat, 23 Nov 1991 15:38:39 GMT
  237. Message-Id: <9111231538.AA13923@voorn.cwi.nl>
  238. To: python-list@cwi.nl
  239. Subject: Re: comments on Python
  240. From: amdcad!bitblks!bvs@uunet.UU.NET
  241. Date: Sat, 23 Nov 91 16:38:38 +0100
  242. Sender: Guido.van.Rossum@cwi.nl
  243.  
  244. [Another repost; ">" is me, plain is Bakul.  --Guido]
  245.  
  246. > I'll try.  Is there some predefined symbol by which I can recognize
  247. > sunos-3 from SunOS 4?
  248.  
  249. I just added #ifdef sun, which is defined.  I don't know if sunOS 4
  250. adds another define to distinguish it from earlier versions.
  251.  
  252. > When I print it the pages are numbered.  Maybe you should shorten the
  253. > page height in "myformat.sty" -- I set it for European paper size,
  254. > which is about 12 inches high, while yours is likely to be 11 inches.
  255.  
  256. Ah, that explains it.  Would be nice if your style worked for
  257. either size (may be we need a4.sty).
  258.  
  259. > Actually, Python lacks explicit declarations as well -- and indeed it
  260.  
  261. True.  I was just adding up perl's sins!  It seems funny that you
  262. have to import definitions but not declare variable!  Even a `def
  263. <var>' will help.  I would also like `def <var> <type>' or
  264. something similar.
  265.  
  266. > Python on purpose gives you one way to do most things (one way for
  267. > each things, not one way for all things together!), on the account
  268. > that it makes the choice easier, and makes programs more readable --
  269.  
  270. Perl does go overboard in the number of choices it provides but
  271. common idioms can become easier to read if they have a special
  272. form.  Even though x[i*j] += y is equiv. to x[i*j] = x[i*j] + y, I
  273. favor the first form.  And the same is true of other idioms.  So
  274. `only one way' seems limiting.
  275.  
  276. > There is a demo directory in the distribution which tries to give the
  277. > examples, and part of the library is also intended as such -- but more
  278. > examples always help.
  279.  
  280. You may also want to post new useful scripts to lang.misc to
  281. stimulate discussion (and may be flames.  But we can ignore those)
  282. and to give people an opportunity to see python in action.
  283. (Mailing lists tend to be overwhelming for beginners).
  284.  
  285. > >I should be able to say
  286. > >       result += b
  287. > >or better still,
  288. > >       result += [a,b,c]
  289. >
  290. > The latter should automatically work if we let x += y be syntactic
  291. > sugar for x = x+y.  Note that result = result + [x] has a different
  292. > meaning than result.append(x)...
  293.  
  294. I understand the difference -- that is why I think overloading
  295. operators is worth considering.  The '+' in result += b is a
  296. different operator from 'result + [a,b,c]'.  As long as the type
  297. signature is different, there should not be a problem in resolving
  298. operators, right?
  299.  
  300. > For simple cases you can use tuples -- this keeps values together but
  301. > you reference them by position.  For larger cases you indeed have to
  302. > use classes -- but what's ugly about that?
  303.  
  304. Because all I want is to name tuple constituents.  A class is a
  305. much heavier weight feature.  I think what is lacking is *syntax*
  306. as in a referentially transparent language you can easily translate
  307. tuple.field to tuple.position.  I suppose you do need declarations.
  308.  
  309. > You can do this!  If a is a list of three elements, just write
  310. >        [x, y, z] = a
  311.  
  312. Good!  Guess I should reread the tutorial!
  313.  
  314. > import posix, string
  315. > pipe = posix.popen('ls -l', 'r')
  316. > for line in pipe.readlines():
  317. >        if line[:5] = 'total': continue
  318. >        [perm,links,user,group,size,mon,day,YoT,name] = string.split(line)[:9]
  319. >        print size, '\t' + name
  320.  
  321. This is exactly the kind of thing that will let people compare
  322. python with other languages.  Shouldn't that `string.split' be
  323. `line.split'?  Why is '\t' + name preferable to '\t', name?
  324.  
  325. > 'total', which makes this kind of scripts so dangerously non-portable.
  326.  
  327. Though that does not depend on the scripting language.
  328.  
  329. > There's already a built-in module 'regexp', which does what you want.
  330. > Access is slightly clumsier than Perl (there's no special syntax to
  331. > handle regular expressions) and the expressions are simpler (just
  332. > Henry Spencer's regexp package), but what you want is there...
  333.  
  334. I should really get reacquainted with python.  It has been while
  335. and my short term memory is getting worse:-(
  336.  
  337. - -- bakul
  338.  
  339. PS: one more comment.  The fact that for built in things you can
  340. use unqualfied procedures (operators?) such as `len(string)' but
  341. for user defined class instances you have to do `foo.len()' seems
  342. inconsistent -- I don't know what can be done though.
  343.  
  344. ------- End of Forwarded Message
  345.  
  346. 
  347. 
  348. Received: by charon.cwi.nl with SMTP; Sat, 23 Nov 1991 17:29:59 +0100
  349. Received: by voorn.cwi.nl with SMTP; Sat, 23 Nov 1991 16:29:58 GMT
  350. Message-Id: <9111231629.AA13987@voorn.cwi.nl>
  351. To: python-list@cwi.nl
  352. Subject: Re: comments on Python 
  353. In-Reply-To: Your message of "Sat, 23 Nov 91 16:38:38 MET."
  354.              <9111231538.AA13923@voorn.cwi.nl> 
  355. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  356. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  357. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  358. Date: Sat, 23 Nov 91 17:29:57 +0100
  359. Sender: Guido.van.Rossum@cwi.nl
  360.  
  361. [Here's my reply to Bakul.  ">>" is me, ">" is Bakul, plain is me again. 
  362. We begin with some boring details... --Guido]
  363.  
  364. >> I'll try.  Is there some predefined symbol by which I can recognize
  365. >> sunos-3 from SunOS 4?
  366. >
  367. >I just added #ifdef sun, which is defined.  I don't know if sunOS 4
  368. >adds another define to distinguish it from earlier versions.
  369.  
  370. I don't know either.  Anybody else on the list know how ti distinguish
  371. SunOS 4.x from 3.x?
  372.  
  373. >> When I print it the pages are numbered.  Maybe you should shorten the
  374. >> page height in "myformat.sty" -- I set it for European paper size,
  375. >> which is about 12 inches high, while yours is likely to be 11 inches.
  376. >
  377. >Ah, that explains it.  Would be nice if your style worked for
  378. >either size (may be we need a4.sty).
  379.  
  380. There are some comments in the style file that explain how to patch it for
  381. US size paper.  Maybe I should distribute it with US size paper turned on
  382. by default.
  383.  
  384. >> Actually, Python lacks explicit declarations as well -- and indeed it
  385. >
  386. >True.  I was just adding up perl's sins!  It seems funny that you
  387. >have to import definitions but not declare variable!  Even a `def
  388. ><var>' will help.  I would also like `def <var> <type>' or
  389. >something similar.
  390.  
  391. I don't believe explicit declarations of local variables is a good idea
  392. for a language intended for casual programming, like Python.  Instead, a
  393. compile-time checker should be written that detects use of undefined
  394. variables orq assignment to variables that are never used.  Remember, *use*
  395. of an undefined variable is a run-time error in Python -- in a sense, the
  396. assignment that gives the variable a value is its declaration.
  397.  
  398. Importing definitions (modules) is the equivalent of assigning a value to
  399. a variable, not of declaring a variable.  Hence there is no inconsistency.
  400.  
  401. >> Python on purpose gives you one way to do most things (one way for
  402. >> each things, not one way for all things together!), on the account
  403. >> that it makes the choice easier, and makes programs more readable --
  404. >
  405. >Perl does go overboard in the number of choices it provides but
  406. >common idioms can become easier to read if they have a special
  407. >form.  Even though x[i*j] += y is equiv. to x[i*j] = x[i*j] + y, I
  408. >favor the first form.  And the same is true of other idioms.  So
  409. >`only one way' seems limiting.
  410.  
  411. Sure.  I'm not using this rule to make Python the absolutely smallest
  412. language possible.  I just prefer to show people that there is already a
  413. way to do what they want (if this is so) rather than adding yet another
  414. feature.  There are actually pretty good reasons to include assignment
  415. operators like "+=" and I'd like to add them.
  416.  
  417. >> There is a demo directory in the distribution which tries to give the
  418. >> examples, and part of the library is also intended as such -- but more
  419. >> examples always help.
  420. >
  421. >You may also want to post new useful scripts to lang.misc to
  422. >stimulate discussion (and may be flames.  But we can ignore those)
  423. >and to give people an opportunity to see python in action.
  424. >(Mailing lists tend to be overwhelming for beginners).
  425.  
  426. I tried this long ago, but stopped because there seemed too little
  427. interest.  Also, it is hard to write good examples that are also useful:
  428. most code either is useful but not written in the absolutely cleanest way
  429. possible, or it is really clean and easy to follow, but lacks useful
  430. options.  Maybe someone could go through the demo directory, pick a few
  431. examples that are clean enough to show to outsiders, and post them...
  432.  
  433. >> >I should be able to say
  434. >> >       result += b
  435. >> >or better still,
  436. >> >       result += [a,b,c]
  437. >>
  438. >> The latter should automatically work if we let x += y be syntactic
  439. >> sugar for x = x+y.  Note that result = result + [x] has a different
  440. >> meaning than result.append(x)...
  441. >
  442. >I understand the difference -- that is why I think overloading
  443. >operators is worth considering.  The '+' in result += b is a
  444. >different operator from 'result + [a,b,c]'.  As long as the type
  445. >signature is different, there should not be a problem in resolving
  446. >operators, right?
  447.  
  448. Unfortunately, this would require deferral of operator resolution to
  449. run-time, and could cause quite a few surprises.  Consider the following
  450. example.  Let's say we have a function to add an element to a list if it
  451. is not already in the list (this could be part of a package emulating set
  452. semantics using lists):
  453.  
  454.     def add_elem(set, elem):
  455.         if elem not in set:
  456.             set += elem
  457.  
  458. Normally, the last line would be interpreted (using your operating
  459. overloading) as set.append(elem).  However, if we had a set of lists, or a
  460. set of sets (quite a common construct in mathematics; lists of lists are
  461. also very useful in Python), it would be interpreted as "set = set +
  462. elem"! In a language with static typing, this cannot occur, but Python has
  463. very dynamic typing...  Changing that would make it an entirely different
  464. language.
  465.  
  466. >> For simple cases you can use tuples -- this keeps values together but
  467. >> you reference them by position.  For larger cases you indeed have to
  468. >> use classes -- but what's ugly about that?
  469. >
  470. >Because all I want is to name tuple constituents.  A class is a
  471. >much heavier weight feature.  I think what is lacking is *syntax*
  472. >as in a referentially transparent language you can easily translate
  473. >tuple.field to tuple.position.  I suppose you do need declarations.
  474.  
  475. I'm still not convinced that it is necessary to this to the language. 
  476. Because of the dynamic typing, the translation of tuple.field to
  477. tuple[position] has to be done at run-time.  The implementation of class
  478. instances already provides such a mapping (using a dictionary).  Why do
  479. you think classes are heavy-weight?  All you need is to place
  480.  
  481.     class Struct(): pass
  482.  
  483. somewhere early in your module, and then you can create structures using
  484.  
  485.     x = struct()
  486.  
  487. and fill them with statements like
  488.  
  489.     x.width = 14
  490.     x.height = 200
  491.  
  492.  
  493. >> You can do this!  If a is a list of three elements, just write
  494. >>        [x, y, z] = a
  495. >
  496. >Good!  Guess I should reread the tutorial!
  497.  
  498. I'm not absolutely sure it's in there (the tuturial is already
  499. overloaded).  It will be in the reference manual I'm writing.
  500.  
  501. >> import posix, string
  502. >> pipe = posix.popen('ls -l', 'r')
  503. >> for line in pipe.readlines():
  504. >>        if line[:5] = 'total': continue
  505. >>        [perm,links,user,group,size,mon,day,YoT,name] = string.split(line)[:9]
  506. >>        print size, '\t' + name
  507. >
  508. >This is exactly the kind of thing that will let people compare
  509. >python with other languages.  Shouldn't that `string.split' be
  510. >`line.split'?  Why is '\t' + name preferable to '\t', name?
  511.  
  512. No it's definitely "string.split(line)".  The point is, for better or for
  513. worse, strings don't have attributes, so all string operations that aren't
  514. built-in operations (like "+" for concatenation) have to be functions. 
  515. The module "string" is a collection of such functions.
  516.  
  517. I wrote '\t' + name because '\t', name inserts an extra space between the
  518. tab and the name (print *always* inserts spaces between output items,
  519. except if an item ends in '\n').
  520.  
  521. >> 'total', which makes this kind of scripts so dangerously non-portable.
  522. >
  523. >Though that does not depend on the scripting language.
  524.  
  525. It does, because sime scripting languages offer no alternative (e.g., in
  526. "sh" scripts or awk, you just have to parse "ls" output for this case,
  527. while Perl and Python also offer a direct interface to the stat() system
  528. call.
  529.  
  530. >> There's already a built-in module 'regexp', which does what you want.
  531. >> Access is slightly clumsier than Perl (there's no special syntax to
  532. >> handle regular expressions) and the expressions are simpler (just
  533. >> Henry Spencer's regexp package), but what you want is there...
  534. >
  535. >I should really get reacquainted with python.  It has been while
  536. >and my short term memory is getting worse:-(
  537.  
  538. Go ahead -- the more you use it, the more you'll like it!
  539.  
  540. >- -- bakul
  541. >
  542. >PS: one more comment.  The fact that for built in things you can
  543. >use unqualfied procedures (operators?) such as `len(string)' but
  544. >for user defined class instances you have to do `foo.len()' seems
  545. >inconsistent -- I don't know what can be done though.
  546.  
  547. len() is a border case: it applies to several different types (all
  548. sequence and dictionary types), some of which currently have no methods
  549. (strings and tuples).  I think len(str) is prettier than str.len(), but
  550. this isn't a very important reason.  On the other hand, a limited number
  551. of unqualified built-in functions is probably necessary -- would you
  552. prefer to write (x + 1).abs() rather than abs(x + 1) ???
  553.  
  554. In any case, this is only done for the most important built-in things. 
  555. Other built-in functionality is accessed using imported (built-in)
  556. modules, but some things just need to be available at all times.  The
  557. alternative, which I consider to be worse, would be to turn them all into
  558. keywords.  The "raise" statement is a border case: I played with the idea
  559. of making it a built-in function.  However, making it a statement puts it
  560. on the same level as "return", "break" and "continue", which also
  561. unconditionally pass control to some outer level.  With "raise" being a
  562. statement, it can profit when the syntax is tightened to disallow code
  563. following such branching statements.  Emacs Python mode should
  564. automatically dedent after one of these (it doesn't yet -- anyone care
  565. enough to patch it?)
  566.  
  567. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  568. Honary Member, Royal Society for Putting Things on Top of Other Things
  569. 
  570. 
  571. Replied: Wed, 04 Dec 91 18:48:33 +0100
  572. Replied: ""Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU> python-list@cwi.nl"
  573. Received: by charon.cwi.nl with SMTP; Wed, 4 Dec 1991 17:44:59 +0100
  574. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa08369;
  575.           4 Dec 91 11:44 EST
  576. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  577.     id AA01673; Wed, 4 Dec 91 11:36:05 EST
  578. Date: Wed, 4 Dec 91 11:36:05 EST
  579. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  580. Message-Id: <9112041636.AA01673@aemsun.med.Virginia.EDU>
  581. To: python-list@cwi.nl
  582. Subject: Why no index for tuples or strings ? 
  583.  
  584.  
  585. Most of the functions that operate on mutable sequences but NOT on 
  586. immutable ones are obviously there because they DO CHANGE the sequence.
  587. BUT: why no  string.index() or tuple.index() ? 
  588.  
  589. Is this just an oversight ? 
  590. If not, what is the reason?
  591. ( Just in case it was the documentation that was wrong, I tried this, and
  592.  it does in fact fail. ) 
  593.  
  594. >>>from string import lowercase
  595. >>>L = []
  596. >>>T = ()
  597. >>>for x in lowercase:
  598. ...  L.append( x )
  599. ...  T = T + ( x, )
  600. ...
  601. >>>L.index('k')            # this works
  602. 10
  603.  
  604. >>>T.index('k')            # these two
  605. >>>lowercase.index('k')        # fail
  606.  
  607.  
  608. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  609.  Steven D. Majewski        University of Virginia Physiology Dept.
  610.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  611.  Voice: (804)-982-0831        1600 Jefferson Park Avenue
  612.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  613. 
  614. 
  615. Received: by charon.cwi.nl with SMTP; Wed, 4 Dec 1991 18:51:22 +0100
  616. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa12252;
  617.           4 Dec 91 12:50 EST
  618. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  619.     id AA01741; Wed, 4 Dec 91 12:42:08 EST
  620. Date: Wed, 4 Dec 91 12:42:08 EST
  621. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  622. Message-Id: <9112041742.AA01741@aemsun.med.Virginia.EDU>
  623. To: python-list@cwi.nl
  624. Subject: WHY PYTHON? 
  625.  
  626.  
  627. Why Python ? 
  628. I mean that question in 2 senses.
  629.  
  630. (1) Why the name "python" ? 
  631.  
  632. (2) What role do you see python as fulfilling or in what direction do 
  633. you anticipate its evolution ?  
  634. ( Perhaps it *can* be "all things to all people", but I wonder if there
  635. aren't some potential conflicts ahead between python as: 
  636.    an interactive environment & command interpreter.
  637.    a command scripting language
  638.    a general purpose scheme-like symbol processing language 
  639.    a GENERAL purpose high level language for everything else
  640.    an object oriented language
  641.    whatever else...
  642. I see python as potentially good in all these roles, but perhaps 
  643. the needs and priorities differ. An interactive language needs
  644. conciseness, even if it creates syntactic irregularity. Regularizing
  645. built-in data types with user-objects, which would seem to be one
  646. desirable direction, might conflict with adding more built-in's 
  647. ( like vectors, for example ) for effeciency. )
  648.  
  649. ---
  650.  One issue I will raise ( re: regularizing built-ins and objects ): 
  651.  I suggest that hooks to change the print representation of an object
  652.  are desirable. Obviously, for user objects, one can write a '.print' 
  653.  method function for the object, and maintain the convention of 
  654.  consistently using that name ( or "._print" ). But it would be better
  655.  if you could override "`" (backquote) for both user objects and 
  656.  built-in data types. COMMON-LISP has this capability: It comes in 
  657.  handy when you are dealing with large vectors/lists/tuples/etc. Instead
  658.  of waiting for your terminal to print out a thousand number, you can 
  659.  change the print representation to be something like: 
  660.   "List of size=1024 : ( 0, 1, 2,  ... 1021, 1022, 1023 )" 
  661.  Minimally, you should be able to change the string backquote returns. 
  662.  Maximally, you should be able to change the action when evaluating a 
  663.  symbol, i.e. , currently typing a symbol causes the equivalent to 
  664.  'print `symbol` ' to occur. Instead of:
  665.    >>> a = [ 1,2,3,4,5 ]
  666.    >>> a
  667.    [ 1, 2, 3, 4, 5 ]
  668.  Typing 'a' could cause a plot of the values in a window. 
  669.  ( Probably this last is too much, and should be left to a user-level
  670.    interpreted written on top of python. )
  671.  
  672.  In general, I think that regularization of built-in data types and
  673.  user written objects is a good direction: allow operators like '+' to 
  674.  be defined for user-objects, and let built-ins be used as classes for
  675.  inheritance. i.e. "class myList() = list: "  
  676.  
  677.  - Steve
  678.  
  679.  
  680. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  681.  Steven D. Majewski        University of Virginia Physiology Dept.
  682.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  683.  Voice: (804)-982-0831        1600 Jefferson Park Avenue
  684.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  685. 
  686. 
  687. Received: by charon.cwi.nl with SMTP; Wed, 4 Dec 1991 18:48:36 +0100
  688. Received: by voorn.cwi.nl with SMTP; Wed, 4 Dec 1991 17:48:34 GMT
  689. Message-Id: <9112041748.AA02757@voorn.cwi.nl>
  690. To: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  691. Cc: python-list@cwi.nl
  692. Subject: Re: Why no index for tuples or strings ? 
  693. In-Reply-To: Your message of "Wed, 04 Dec 91 11:36:05 MET."
  694.              <9112041636.AA01673@aemsun.med.Virginia.EDU> 
  695. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  696. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  697. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  698. Date: Wed, 04 Dec 91 18:48:34 +0100
  699. Sender: Guido.van.Rossum@cwi.nl
  700.  
  701. >Most of the functions that operate on mutable sequences but NOT on 
  702. >immutable ones are obviously there because they DO CHANGE the sequence.
  703. >BUT: why no  string.index() or tuple.index() ? 
  704. >
  705. >Is this just an oversight ? 
  706. >If not, what is the reason?
  707.  
  708. Umm, there isn't a real good reason.  One thing I can say in my
  709. defense is that string and tuple objects have no methods at all, all
  710. operations on these are done with built-in operations like "+" and
  711. "[...]", so adding an "index" method would be a bit of a change in the
  712. structure.
  713.  
  714. For tuples, I suspect such a function would rarely be used; I think
  715. that is most cases where x.index() would be useful, x is generally a
  716. list, whose contents varies in time, rather than a tuple (which cannot
  717. change easily).
  718.  
  719. For strings, there is a built-in module "string" which exports a
  720. function "index" which searches for substrings, so you can say
  721.  
  722.     string.index('one two three', 'two')
  723.  
  724. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  725. "Oh my god, he's fallen off the edge of the cartoon."
  726. 
  727. 
  728. Received: by charon.cwi.nl with SMTP; Wed, 4 Dec 1991 20:02:44 +0100
  729. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa16972;
  730.           4 Dec 91 14:02 EST
  731. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  732.     id AA01825; Wed, 4 Dec 91 13:54:11 EST
  733. Date: Wed, 4 Dec 91 13:54:11 EST
  734. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  735. Message-Id: <9112041854.AA01825@aemsun.med.Virginia.EDU>
  736. To: python-list@cwi.nl
  737. Subject: A useful quickie function to read data.
  738.  
  739. I am already finding python quite useful. 
  740. I'll wait till I debug them before I post any full programs,
  741. ( I've got a 'du' in python that does't follow links or mount points
  742.   and takes a max-depth and/or a prune-path argument to limit 
  743.   how detailed a listing it generates. There is some bug with 
  744.   the max-depth, and I haven't had time to get back to it. )
  745. but here is a quickie function that is useful.
  746. I am using python to do some quick calculations. I found it easy to
  747. enter the data in "python" format, and use the following function 
  748. to load the data. The data looks like this: 
  749.  
  750. ( 29, 'Mg-Cl2',     (('Mg', 1, 0.66099e+6),     ('Cl', 2, 0.12413e+7 )))
  751. ( 31, 'Ca-S-O4',     (('Ca', 1, 0.11708e+7),     ('S',  1, 0.83657e+6 )))
  752. ( 32, 'Mg-S-O4',     (('Mg', 1, 0.41470e+6),  ('S',  1, 0.28843e+6 )))
  753. ( 33, 'Ca-Cl2',     (('Ca', 1, 0.64138e+6),  ('Cl', 2, 0.81340e+6 )))
  754. ( 34, 'Al2-S-O4',     (('Al', 2, 0.11451e+6 ), ('S',  1, 0.17072e+6 )))
  755. ( 35, 'Na2-S-O4',     (('Na', 2, 0.41116e+6 ), ('S',  1, 0.44132e+6 )))
  756. ( 36, 'K-H2-P-O4',      (('K',  1, 0.11479e+7 ), ('P',  1, 0.89652e+6 )))
  757. ( 37, 'Na4-P2-O7',     (('Na', 4, 0.12013e+7 ), ('P',  2, 0.10849e+7 )))
  758. ( 73, 'K-Cl',          (('K',  1, 0.45241e+6),  ('Cl', 1, 0.20116e+6 )))
  759. ( 72, 'Ca3-P-O4',     (('Ca', 3, 0.17659e+7),  ('P',  1, 0.70474e+6 )))
  760.  
  761. And is read into a list with: 
  762.  
  763. >>>f = open( filename, 'r' )
  764. >>>L = []
  765. >>> while 1:
  766. ...   n = feval( b )
  767. ...   if n = '' :
  768. ...     break
  769. ...   else:
  770. ...     L.append(n)
  771. ...
  772.  
  773.  
  774. Note: 
  775.      It is a snap in python to generate tuple pairs like: ( 'Ca/P', ratio ),
  776.      take a list of those pairs,  invert all of the tuples to:
  777.      ( 'P/Ca', 1.0/ratio ), merge and sort the 2 lists. I'm working on 
  778.      the "given any one absolute value, calculate the others from the
  779.      ratio pairs" routine. I just took some time off to read the note 
  780.      on classes that is in the 'misc' directory and reorganize it as 
  781.      more Obj-Or. I note this mainly to stress how easy to learn the 
  782.      language is, and how quickly one can start doing useful work. 
  783.      [ As a counter-example, I remember when I first tried to use 
  784.        LISP for something other than a textbook exercise, I spent days
  785.        re-inventing functions that were actually in the library, but were 
  786.        too obscurely named/indexed/documented to find. ]
  787.  
  788. Also note: sys.exc_type not in the library reference. I found it with
  789.    dir(sys). Several times I have been ready to write a "why doesn't
  790.    python have" or "why can't you" message, only to find, with a little
  791.    poking around that what I needed *was* there somewhere. 
  792.  
  793.  
  794. #! /usr/local/python 
  795. # (no main - not executable - above is just there for unix 'file' cmd )
  796. #
  797. # feval( file ) 
  798. # evaluates a line from file
  799. # Intended for loading python formatted data from a file
  800. # Will not work for expressions longer than a line.
  801. # Should modify and make a version that reads multi-line statements
  802. # but 'input' seems to only work on one liners, so this is 
  803. # consistent. Should also try a version using a symbol table 
  804. # arg to eval. ( i.e. 
  805. #            >>> a = 1
  806. #            >>> b = input( '?:' )
  807. #            ?: a
  808. #        Fails: a is not defined within the scope of input()
  809. # unless you also pass a symbol table. ) 
  810. #
  811.  
  812. from string import strip
  813. import sys
  814.  
  815. def feval( File ):
  816.   try:
  817.     line = File.readline()
  818.     if line = '' :  return line ;
  819.     return  eval( strip( line ))
  820.   except EOFError: 
  821.     return EOFError
  822.   except  ( TypeError,NameError,RuntimeError), what:
  823.     return ( sys.exc_type, what, strip(line))
  824.  
  825.   
  826. ########
  827. # note: 
  828. # This also works:
  829. # reassign sys.stdin and use the input() function.
  830. # Should probably wrap a try: except: finally: around 
  831. # it so it can't terminate without restoring sys.stdin
  832.  
  833. >>> import sys
  834. >>> def infrom( f ):
  835. ...    saved = sys.stdin
  836. ...    sys.stdin = f
  837. ...    tmp = input( `f`+':\n' )
  838. ...    sys.stdin = saved
  839. ...    return tmp
  840. ...
  841. >>> b = open( 'binaries', 'r' )
  842. >>> b = infrom( b )
  843.  
  844.  
  845.  - Steve Majewski
  846.  
  847.  
  848.  
  849.  
  850. 
  851. 
  852. Received: by charon.cwi.nl with SMTP; Wed, 4 Dec 1991 23:53:44 +0100
  853. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa01338;
  854.           4 Dec 91 17:53 EST
  855. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  856.     id AA02479; Wed, 4 Dec 91 17:45:05 EST
  857. Date: Wed, 4 Dec 91 17:45:05 EST
  858. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  859. Message-Id: <9112042245.AA02479@aemsun.med.Virginia.EDU>
  860. To: python-list@cwi.nl
  861. Subject: passing/catching a variable number of args in python
  862.  
  863.  
  864. Arguments are passed as tuples, except in the case where there is one
  865. or zero arguments. 
  866.     f( 1,2,3 )        *is the*         f( (1,2,3) )
  867.         f( 1,2 )        *same as*        f( (1,2) )
  868.                   *BUT* 
  869. CASE 1:    f( 1 ) or f( (1) )     *is NOT*    f( 1, ) or f( (1,) )
  870.                     *nor is*
  871. CASE 0:    f( )            *equiv.to*    f( () )
  872.  
  873. This works to pass a variable number of arguments, except in the 
  874. case of NO arguments, which generates an error. Is there any way
  875. around this other than wrapping a try:except: around it to catch
  876. the type error for zero arguments ? 
  877.  
  878.  
  879. >>> def who( x ):
  880. ...   print type( x )
  881. ...   if type(x) <> type( () ):  #this takes care of CASE 1
  882. ...     x = x,
  883. ...   print len(x)
  884. ...   print x
  885. ...
  886. >>> who( 1,2,3 )
  887. <type 'tuple'>
  888. 3
  889. (1, 2, 3)
  890. >>> who( 1, )        # CASE 1:  tuple of len = 1 
  891. <type 'tuple'>
  892. 1
  893. (1,)
  894. >>> who( 1 )        # CASE 1:
  895. <type 'int'>        # coerced into a tuple.
  896. 1
  897. (1,)    
  898. >>> who( () )        # CASE 0:
  899. <type 'tuple'>
  900. 0
  901. ()
  902. >>> who( )        
  903. Unhandled exception: type error: function expects argument(s)
  904. Stack backtrace (innermost last):
  905.   File "<stdin>", line 1
  906.   File "<stdin>", line 1
  907.  
  908.  
  909. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  910.  Steven D. Majewski        University of Virginia Physiology Dept.
  911.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  912.  Voice: (804)-982-0831        1600 Jefferson Park Avenue
  913.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  914. 
  915. 
  916. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 11:37:58 +0100
  917. Received: by voorn.cwi.nl with SMTP; Fri, 6 Dec 1991 10:37:58 GMT
  918. Message-Id: <9112061037.AA06793@voorn.cwi.nl>
  919. To: python-list@cwi.nl
  920. Subject: Re: WHY PYTHON? 
  921. In-Reply-To: Your message of "Wed, 04 Dec 91 12:42:08 MET."
  922.              <9112041742.AA01741@aemsun.med.Virginia.EDU> 
  923. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  924. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  925. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  926. Date: Fri, 06 Dec 91 11:37:56 +0100
  927. Sender: Guido.van.Rossum@cwi.nl
  928.  
  929. >(1) Why the name "python" ? 
  930.  
  931. I an a fan of Monty Python's Flying Circus, and I hate acronyms.  Other than
  932. that, there's nothing deep.
  933.  
  934. >(2) What role do you see python as fulfilling or in what direction do 
  935. >you anticipate its evolution ?  
  936. >( Perhaps it *can* be "all things to all people", but I wonder if there
  937. >aren't some potential conflicts ahead between python as: 
  938. >   an interactive environment & command interpreter.
  939. >   a command scripting language
  940. >   a general purpose scheme-like symbol processing language 
  941. >   a GENERAL purpose high level language for everything else
  942. >   an object oriented language
  943. >   whatever else...
  944. >I see python as potentially good in all these roles, but perhaps 
  945. >the needs and priorities differ. An interactive language needs
  946. >conciseness, even if it creates syntactic irregularity. Regularizing
  947. >built-in data types with user-objects, which would seem to be one
  948. >desirable direction, might conflict with adding more built-in's 
  949. >( like vectors, for example ) for effeciency. )
  950.  
  951. I could go on for hours on this subject, but I'll keep it short this time :-)
  952.  
  953. The one thing that Python definitely does not want to be is a GENERAL purpose
  954. programming language.  Its lack of declarations and general laziness about
  955. compile-time checking is definitely aimed at small-to-medium-sized programs.
  956.  
  957. As an interactive environment and command interpreter, it leaves a lot to wish
  958. -- the bash history was really a hack to quickily get some kind of line
  959. editing.  I hope I can one day implement a better interactive environment in
  960. Python -- there's a module "python.py" the demo/stdwin directory that shows a
  961. little bit of how much can be done without changing the interpreter.
  962.  
  963. Object-oriented languages are a means, not a goal in themselves, so, yes,
  964. Python is object-oriented (and more so than most other languages that claim
  965. this adjective) but its goal in life is not to be OO.
  966.  
  967. I don't know what you mean by symbol-processing language.  If you just mean
  968. non-numerical computations, fine.
  969.  
  970. Looking in my crystal ball, I think that Python may develop stronger support
  971. for medium-sized programs (e.g., some more compile-time error checking) but
  972. won't lose a bit of its attractiveness for very short programs -- like scripts.
  973.  
  974. > One issue I will raise ( re: regularizing built-ins and objects ): 
  975. > I suggest that hooks to change the print representation of an object
  976. > are desirable. Obviously, for user objects, one can write a '.print' 
  977. > method function for the object, and maintain the convention of 
  978. > consistently using that name ( or "._print" ). But it would be better
  979. > if you could override "`" (backquote) for both user objects and 
  980. > built-in data types. COMMON-LISP has this capability: It comes in 
  981. > handy when you are dealing with large vectors/lists/tuples/etc. Instead
  982. > of waiting for your terminal to print out a thousand number, you can 
  983. > change the print representation to be something like: 
  984. >  "List of size=1024 : ( 0, 1, 2,  ... 1021, 1022, 1023 )" 
  985. > Minimally, you should be able to change the string backquote returns. 
  986. > Maximally, you should be able to change the action when evaluating a 
  987. > symbol, i.e. , currently typing a symbol causes the equivalent to 
  988. > 'print `symbol` ' to occur. Instead of:
  989. >   >>> a = [ 1,2,3,4,5 ]
  990. >   >>> a
  991. >   [ 1, 2, 3, 4, 5 ]
  992. > Typing 'a' could cause a plot of the values in a window. 
  993. > ( Probably this last is too much, and should be left to a user-level
  994. >   interpreted written on top of python. )
  995. >
  996. > In general, I think that regularization of built-in data types and
  997. > user written objects is a good direction: allow operators like '+' to 
  998. > be defined for user-objects, and let built-ins be used as classes for
  999. > inheritance. i.e. "class myList() = list: "  
  1000.  
  1001. I don't think I agree that the level of modification of existing built-in
  1002. operations like you propose is a good thing.
  1003.  
  1004. For example, let's take backquotes.  The problem is that not all uses of
  1005. backquote are used to write data to the terminal: as long as you don't use any
  1006. "weird" objects like files, the string output by `expr` is guaranteed to be a
  1007. valid Python expression that evaluates (using eval(), for instance) to an
  1008. object with the same value (approximately, in the case of floats).
  1009.  
  1010. *If*, and I say *if* there will ever be a mechanism whereby users can modify
  1011. built-in object types, I think it will be likely a way to add new methods to
  1012. built-in classes.  A way to define standard operators like "+" for user-defined
  1013. types (classes) is also quite feasible -- I can imagine that a class can define
  1014. methods named _operator_plus, _operator_subscript, and so on (there must then
  1015. also be a way to indicate that a class implements a sequence, mapping or
  1016. numeric type).
  1017.  
  1018. Using built-in classes as base classes for user-defined classes poses several
  1019. problems, most of which lie in the realm of implementation.  Several other OO
  1020. languages can live without this (C++, Modula-3), so I hope Python can as well.
  1021.  
  1022. Thank you for raising these issues,
  1023.  
  1024. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1025. "Is it a bird? No! Is it a plane? No! It's bicycle repair man!"
  1026. 
  1027. 
  1028. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 11:43:43 +0100
  1029. Received: by voorn.cwi.nl with SMTP; Fri, 6 Dec 1991 10:43:42 GMT
  1030. Message-Id: <9112061043.AA06806@voorn.cwi.nl>
  1031. To: python-list@cwi.nl
  1032. Subject: Re: A useful quickie function to read data. 
  1033. In-Reply-To: Your message of "Wed, 04 Dec 91 13:54:11 MET."
  1034.              <9112041854.AA01825@aemsun.med.Virginia.EDU> 
  1035. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1036. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  1037. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  1038. Date: Fri, 06 Dec 91 11:43:41 +0100
  1039. Sender: Guido.van.Rossum@cwi.nl
  1040.  
  1041. >Also note: sys.exc_type not in the library reference. I found it with
  1042. >   dir(sys). Several times I have been ready to write a "why doesn't
  1043. >   python have" or "why can't you" message, only to find, with a little
  1044. >   poking around that what I needed *was* there somewhere. 
  1045.  
  1046. Scanning the table-of-contents of the library reference manual is always a
  1047. good way to start.  One day I'll prepare a full-blown index...  (But first
  1048. the reference manual must be completed.)
  1049.  
  1050. ># but 'input' seems to only work on one liners,
  1051.  
  1052. Actually, input() directly calls the lexical scanner, and accepts a
  1053. backslash to indicate continuation lines.
  1054.  
  1055. >#            >>> a = 1
  1056. >#            >>> b = input( '?:' )
  1057. >#            ?: a
  1058. >#        Fails: a is not defined within the scope of input()
  1059. ># unless you also pass a symbol table. ) 
  1060.  
  1061. This example assigns 1 to b if I try it.  The default environment where
  1062. input() and eval()/exec() work is that where the call is.
  1063.  
  1064. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1065. "It's probably pining for the fiords"
  1066. 
  1067. 
  1068. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 14:42:07 +0100
  1069. Received: by voorn.cwi.nl with SMTP; Fri, 6 Dec 1991 13:42:06 GMT
  1070. Message-Id: <9112061342.AA07033@voorn.cwi.nl>
  1071. To: python-list@cwi.nl
  1072. Subject: Re: passing/catching a variable number of args in python 
  1073. In-Reply-To: Your message of "Wed, 04 Dec 91 17:45:05 MET."
  1074.              <9112042245.AA02479@aemsun.med.Virginia.EDU> 
  1075. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1076. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  1077. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  1078. Date: Fri, 06 Dec 91 14:42:05 +0100
  1079. Sender: Guido.van.Rossum@cwi.nl
  1080.  
  1081. >Arguments are passed as tuples, except in the case where there is one
  1082. >or zero arguments. 
  1083.  
  1084. A more appropriate way of saying this is: there is always exactly 0 or 1
  1085. argument.  When there appear to be more, there really is a single argument
  1086. which happens to be a tuple.  You have more or less discovered this.
  1087.  
  1088. The exception for 0 arguments is unfortunate.  I have played with the
  1089. thought of making f() and f(()) equivalent, but...  there is a complication
  1090. with class methods.  When C is a class defining a method m with argument
  1091. list (self, args), and c an instance of C, then calling c.m(x) is equivalent
  1092. C.m(c, x).  However, argument-less methods are conventionally defined as
  1093. m(self), and here c.m() is equivalent to C.m(c).  If f() and f(()) were to
  1094. be equivalent, all argument-less methods would have to be rewritten to
  1095. require a second argument, which would always receive the value ().
  1096.  
  1097. (Part of all this is caused by the notion that there is *nothing special*
  1098. about a method in the way you declare it; purely the fact that a class
  1099. attribute happens to be a function object makes it a method.  This
  1100. simplifies life in several other places, and gives a nice way of
  1101. constructing a class out of existing functions.)
  1102.  
  1103. A different approach would appear to *always* make function arguments a
  1104. tuple.  This would pass single arguments as singleton tuples, making f(1)
  1105. equivalent to f(1,).  However, this doesn't fly: in the current situation,
  1106. you can write x = (1,2,3) and later call f(x) with exactly the same result
  1107. as f(1,2,3).  In the alternate approach, f(x) would be turned into f(x,),
  1108. and would no longer be equivalent to f(1,2,3).  So either way you lose.
  1109.  
  1110. Knowing the argument structure of the called function would also be a
  1111. possibility, but this definitely has to be done at run-time (when the call
  1112. is executed), not at "compile-time" (when it is parsed and turned into
  1113. internal opcodes), since there is also a (desirable!) equivalence between
  1114.  
  1115.     def f(x):
  1116.         a, b = x
  1117.         "use a and b"
  1118.  
  1119. and
  1120.  
  1121.     def f(a, b):
  1122.         "use a and b"
  1123.  
  1124. So it seems we're stuck.  The most realistic solution in my eyes is to
  1125. require methods to always have exactly two arguments, the second one
  1126. receiving a dummy () value in case of argument-less methods, but this cannot
  1127. be introduced without breaking old code.  (Is there any old code outside my
  1128. own that uses this?  I'm always willing to change the language if it breaks
  1129. nobody's code but mine -- including the library).
  1130.  
  1131. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1132. "I'm sorry, is this a five-minute argument, or the full half-hour?"
  1133. 
  1134. 
  1135. Replied: Sat, 07 Dec 91 12:43:43 +0100
  1136. Replied: python-list@cwi.nl
  1137. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 17:24:59 +0100
  1138. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA15756
  1139.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Fri, 6 Dec 91 10:19:56 -0600
  1140. Return-Path: <liberte@ncsa.uiuc.edu>
  1141. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  1142.     id AA01564; Fri, 6 Dec 91 10:19:04 CST
  1143. Date: Fri, 6 Dec 91 10:19:04 CST
  1144. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  1145. Message-Id: <9112061619.AA01564@void.ncsa.uiuc.edu>
  1146. To: Guido.van.Rossum@cwi.nl, python-list@cwi.nl
  1147. Subject: Re: passing/catching a variable number of args in python
  1148.  
  1149.     A different approach would appear to *always* make function arguments
  1150.     a tuple.  This would pass single arguments as singleton tuples, making
  1151.     f(1) equivalent to f(1,).  However, this doesn't fly: in the current
  1152.     situation, you can write x = (1,2,3) and later call f(x) with exactly
  1153.     the same result as f(1,2,3).  In the alternate approach, f(x) would be
  1154.     turned into f(x,), and would no longer be equivalent to f(1,2,3).  So
  1155.     either way you lose.
  1156.  
  1157. You could support calling f(x) by using a separate kind of function call,
  1158. analogous to Lisp apply.  f(x) as compared with f(1, 2, 3) really does
  1159. a different thing internally, and it was confusing to me as a
  1160. new user to discover that these were intended to have the same result.
  1161.  
  1162. Maybe you want to consider calling f with arguments being the elements
  1163. of the tuple x using the syntax "f x".  Then "f (1, 2, 3)" is really
  1164. the same kind of function call as "f x".  This may be inconsistent with other
  1165. aspects of the Python syntax; I dont have my grammar handy.
  1166.  
  1167. Dan LaLiberte
  1168. National Center for Supercomputing Applications
  1169.  
  1170. liberte@ncsa.uiuc.edu
  1171. (Join the League for Programming Freedom: league@prep.ai.mit.edu)
  1172. 
  1173. 
  1174. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 19:07:06 +0100
  1175. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa17630;
  1176.           6 Dec 91 13:06 EST
  1177. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  1178.     id AA04610; Fri, 6 Dec 91 12:58:35 EST
  1179. Date: Fri, 6 Dec 91 12:58:35 EST
  1180. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  1181. Message-Id: <9112061758.AA04610@aemsun.med.Virginia.EDU>
  1182. To: Guido.van.Rossum@cwi.nl
  1183. Subject: Re: WHY PYTHON?
  1184.  
  1185. sdm>>
  1186. >>One issue I will raise ( re: regularizing built-ins and objects ): 
  1187. >>I suggest that hooks to change the print representation of an object
  1188. >>are desirable. Obviously, for user objects, one can write a '.print' 
  1189. >>method function for the object, and maintain the convention of 
  1190. >>consistently using that name ( or "._print" ). But it would be better
  1191. >>if you could override "`" (backquote) for both user objects and 
  1192. >>built-in data types.
  1193. >> ... 
  1194. >>In general, I think that regularization of built-in data types and
  1195. >>user written objects is a good direction: allow operators like '+' to 
  1196. >>be defined for user-objects, and let built-ins be used as classes for
  1197. >>inheritance. i.e. "class myList() = list: "  
  1198.  
  1199. Guido>
  1200. >I don't think I agree that the level of modification of existing built-in
  1201. >operations like you propose is a good thing.
  1202. >
  1203. >For example, let's take backquotes.  The problem is that not all uses of
  1204. >backquote are used to write data to the terminal: as long as you don't use any
  1205. >"weird" objects like files, the string output by `expr` is guaranteed to be a
  1206. >valid Python expression that evaluates (using eval(), for instance) to an
  1207. >object with the same value (approximately, in the case of floats).
  1208. >
  1209.  
  1210. After your explaination of the logic behind backquote, I agree that my 
  1211. proposed change is probably undesirable ( for backquote, that is. )
  1212. But if not for "`object`", then maybe for "print thing" ? 
  1213.  
  1214. >
  1215. >*If*, and I say *if* there will ever be a mechanism whereby users can modify
  1216. >built-in object types, I think it will be likely a way to add new methods to
  1217. >built-in classes. A way to define standard operators like "+" for user-defined
  1218. >types (classes) is also quite feasible - I can imagine that a class can define
  1219. >methods named _operator_plus, _operator_subscript, and so on (there must then
  1220. >also be a way to indicate that a class implements a sequence, mapping or
  1221. >numeric type).
  1222. >
  1223.  
  1224. I prefer (all things being equal) regularity/orthogonality and logical
  1225. syntax/semantics in a language because there is less to have to remember.
  1226. ( Of course I *know* all things are NEVER really equal! )
  1227.  
  1228. I can certainly live without these enhancements, or if I really need 
  1229. them, implement them with my own classes on top of python. But I agree
  1230. with what you said about not wanting a dozen different ways of doing 
  1231. the same thing ( as in perl, for example. I think it just makes it 
  1232. more confusing to learn. ). 
  1233.  
  1234. I can make my own classes to duplicate the builtin's: ( and make a 
  1235. hierarchy: mySequence(), myMutableSeq() = mySequence, myList() = 
  1236. myMutableSeq(), etc. and give them all .string() methods that 
  1237. return a print representation. And either do "print thing.string()"
  1238. or make a write routine that checks the type or each arg and if 
  1239. it is an object instance, uses that objects .string() method, else
  1240. uses the object itself. 
  1241. But all this takes maintaining some conventions. I think that is 
  1242. easier if the language encourages some conventions itself. 
  1243.  
  1244. One such convention could be that (builtin) print does that checking: 
  1245. if object instance, check to see it there is a method named 
  1246. "_PrintRepresentation()" , and if there is, call that to get the 
  1247. print string.
  1248.  
  1249. Note: this would not be necessary if "print" was user re-definable, 
  1250. but it appears to be a statement, not a function. I don't know any
  1251. Modula-3, but I did do some Modula-2 programming at one time, and 
  1252. I remember that some of the logic behind its design was the fact 
  1253. that there were things in Pascal that were not implementable *IN* 
  1254. Pascal for this very reason. ( I'm not convinced that the modula-2
  1255. solution was the right one - is that the reason for modula-3 ? )
  1256. ( Of course in python, "write" is a file method and doesn't have 
  1257.  the pecularity of "print" )
  1258.  
  1259.  
  1260. Guido>
  1261. >Using built-in classes as base classes for user-defined classes poses several
  1262. >problems, most of which lie in the realm of implementation.  Several other OO
  1263. >languages can live without this (C++, Modula-3), so I hope Python can as well.
  1264. >
  1265.  
  1266. I can "live" with python just the way it is! 
  1267. I find it much preferable to Perl in both its syntax, modularity and
  1268. interactiveness; easier and more high-level than C , and immensely
  1269. more powerful that csh/ksh/etc,  although it is not concise enough
  1270. to COMPLETELY replace csh, but as I said before, you can't be all 
  1271. things to all people. 
  1272.  
  1273. It's only because it is SO good that I sometimes expect it to be 
  1274. perfect! ( And sometimes I have a wrong idea of what perfect would be. )
  1275.  
  1276. These are just minor quibbles.
  1277. And if the answer DOES come down to  just developing some coding 
  1278. and method/class name conventions, then lets discuss THAT now and 
  1279. develop some example code to ENCOURAGE those conventions.
  1280.  
  1281. - Steve 
  1282.  
  1283. 
  1284. 
  1285. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 19:55:40 +0100
  1286. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa20738;
  1287.           6 Dec 91 13:55 EST
  1288. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  1289.     id AA04713; Fri, 6 Dec 91 13:46:55 EST
  1290. Date: Fri, 6 Dec 91 13:46:55 EST
  1291. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  1292. Message-Id: <9112061846.AA04713@aemsun.med.Virginia.EDU>
  1293. To: python-list@cwi.nl
  1294. Subject: Global variables, assignment, ( & eval )
  1295.  
  1296.  
  1297. Re: input()
  1298.  
  1299. You are correct. 
  1300.  
  1301.    >>> a = 1
  1302.    >>> b = input( '?:' )
  1303.    ?: a 
  1304.    >>> b 
  1305.    1 
  1306.  
  1307. works correctly. 
  1308.  
  1309. I must have had some other syntax errors and did not 
  1310. transcribe it correctly. ( In the longer examples I
  1311. usually cut & past from the screen so I get it verbatim. )
  1312. Sorry.
  1313.  
  1314. I'm also sorry because I thought that your reply pointed to 
  1315. a problem with *my* executable of python, and I thought it 
  1316. might explain some other behaviour that perplexes me. 
  1317.  
  1318.  
  1319. I've been having some trouble with global assignment. 
  1320.  
  1321.  
  1322. ./misc/CLASSES : 
  1323.  "A special quirk of Python is that assignments always go into the
  1324.  innermost scope.  Assignments do not copy data -- they just
  1325.  bind names to objects.  The same is true for deletions: the statement
  1326.  "del x" removes the binding of x from the name space referenced by the
  1327.  local scope.  In fact, all operations that introduce new names use the
  1328.  local scope: in particularly, import statements and function
  1329.  definitions bind the module or function name in the local scope."
  1330.  
  1331.  Does this mean that global variables in a module are READONLY. 
  1332.  That didn't seem to be explicitly stated in the docs. ( Maybe I 
  1333.  missed it. I looked through some of the demo's and most of the
  1334.  globals I saw WERE being used READONLY )
  1335.  
  1336.  I guess a quick experiment answers my own question:
  1337.  
  1338. >>> N = 1000
  1339. >>> def decN():
  1340. ...   print N
  1341. ...   N = N - 1
  1342. ...   print N
  1343. ...   return N
  1344. ...
  1345. >>> N
  1346. 1000
  1347. >>> decN()
  1348. 1000
  1349. 999
  1350. 999
  1351. >>> print N
  1352. 1000
  1353.  
  1354.   ... but I would suggest you stress this a bit more in the tutorial. 
  1355.  
  1356. Do I have to create a class instance to have a writeable non local variable ? 
  1357.  
  1358.  ( I mentioned a bug in my du routine - I can't set MAXDEPTH correctly.
  1359.    This seems to be it. ) 
  1360.  
  1361.  
  1362.   The following is actually the perplexing part: 
  1363.   ( The above is not confusing once I did the experiment, but I'm 
  1364.     not sure I understand the cause of the errors below. Is the 
  1365.     compiler/interpreter anticipating that since I do an assignment,
  1366.     that the references in incK() are to a different K than the 
  1367.     references in access(), and so the print statement before the
  1368.     assignment give me an error ?  
  1369.     Could you perhaps give us a bit more on binding & scope of 
  1370.     variables, etc. ?  )
  1371.  
  1372.  
  1373. # module test
  1374. #
  1375. N = 99
  1376. S = 'This is a test: '
  1377. K = 1
  1378.  
  1379. def access():
  1380.   print N, S, K 
  1381.   return ( N, S, K )
  1382.  
  1383. def newK(x):
  1384.   K = x
  1385.   return K
  1386.  
  1387. def decN():
  1388.   print N
  1389.   N = N - 1
  1390.   return N
  1391.  
  1392. def incK():
  1393.   print K
  1394.   K = K + 1
  1395.   return K
  1396.  
  1397.  
  1398.  
  1399. >>> import test
  1400. >>> test.K
  1401. 1
  1402. >>> test.access()
  1403. 99 This is a test:  1
  1404. (99, 'This is a test: ', 1)
  1405. >>> test.newK( 33 )
  1406. 33
  1407. >>> test.access()
  1408. 99 This is a test:  1
  1409. (99, 'This is a test: ', 1)
  1410. >>> test.K
  1411. 1
  1412. >>> test.incK()
  1413. Unhandled exception: undefined name: K
  1414. Stack backtrace (innermost last):
  1415.   File "<stdin>", line 1
  1416.   File "./test.py", line 21
  1417.     print K
  1418. >>> K = 3        # just to check that it is not GLOBAL/global 
  1419. >>> test.incK()    
  1420. Unhandled exception: undefined name: K
  1421. Stack backtrace (innermost last):
  1422.   File "<stdin>", line 1
  1423.   File "./test.py", line 21
  1424.     print K
  1425.  
  1426.  
  1427.  
  1428.  
  1429. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  1430.  Steven D. Majewski        University of Virginia Physiology Dept.
  1431.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  1432.  Voice: (804)-982-0831        1600 Jefferson Park Avenue
  1433.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  1434. 
  1435. 
  1436. Replied: Sat, 07 Dec 91 12:18:55 +0100
  1437. Replied: python-list@cwi.nl
  1438. Received: by charon.cwi.nl with SMTP; Fri, 6 Dec 1991 20:40:00 +0100
  1439. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa23622;
  1440.           6 Dec 91 14:39 EST
  1441. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  1442.     id AA04791; Fri, 6 Dec 91 14:31:14 EST
  1443. Date: Fri, 6 Dec 91 14:31:14 EST
  1444. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  1445. Message-Id: <9112061931.AA04791@aemsun.med.Virginia.EDU>
  1446. To: python-list@cwi.nl
  1447. Subject: Last message ( Scope & globals & binding ) 
  1448.  
  1449.  
  1450. That was probably an incorrect statement to say that globals are
  1451. READONLY.  The problems is not with globals but with assignment. 
  1452. i.e. One reason this wasn't clear sooner was that I did not run
  1453. into this problem with globals that were LISTS because I did not
  1454. assign to them, but used the access method .append(). So therefore
  1455. global LISTS were updated correctly. 
  1456. I used assignment for scalars. 
  1457.  
  1458. Classes, BTW, do work: 
  1459.  
  1460. class Global():
  1461.   pass
  1462.  
  1463. G = Global()
  1464. G.k = 1000
  1465.  
  1466. def decK():
  1467.   G.k = G.k - 1
  1468.   return G.k
  1469.  
  1470. ... etc... 
  1471. Works as an assignable global variable. 
  1472.  
  1473. Alternately, scalars could be one element lists, and assignment
  1474. done with replacement of the list element. 
  1475. ( Or put all (module wide) globals into a dictionary and replace
  1476.   the values ? - I haven't tried that yet. ) 
  1477.  
  1478. Possibly my problems originated with a poor design in the first place - 
  1479. ( Stuffing things into globals was a quick hack to test an additional 
  1480.   feature. ) But I do think those issues DO need to be made clear(er)
  1481.  in the user-guide. Reading the misc/CLASSES doc helped, but I don't 
  1482.  think the logic is explicitly spelled out in the user guide. 
  1483.  
  1484. - Steve
  1485. 
  1486. 
  1487. Received: by charon.cwi.nl with SMTP; Sat, 7 Dec 1991 00:29:55 +0100
  1488. Received: by mcsun.EU.net via EUnet;
  1489.     id AA25800 (5.65a/CWI-2.128); Sat, 7 Dec 1991 00:29:54 +0100
  1490. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  1491.     (5.61/UUNET-internet-primary) id AA06367; Fri, 6 Dec 91 18:22:38 -0500
  1492. Received: from sparky.UUCP by uunet.uu.net with UUCP/RMAIL
  1493.     (queueing-rmail) id 182126.17180; Fri, 6 Dec 1991 18:21:26 EST
  1494. Received: by sparky.IMD.Sterling.COM (4.1/smail2.5/04-08-90)
  1495.     id AA05133; Fri, 6 Dec 91 17:22:18 CST
  1496. From: rick@sparky.IMD.Sterling.COM (Richard Ohnemus)
  1497. Message-Id: <9112062322.AA05133@sparky.IMD.Sterling.COM>
  1498. Subject: Please add me to mailing list
  1499. To: python-list-request@cwi.nl
  1500. Date: Fri, 6 Dec 91 17:22:18 CST
  1501. X-Mailer: ELM [version 2.3 PL2]
  1502.  
  1503. Please add me to the python mailing list. Thanks...
  1504. -- 
  1505. Rick Ohnemus   INTERNET: rick@sparky.IMD.Sterling.COM   UUCP: uunet!sparky!rick
  1506. LAND: Sterling Software IMD, 1404 Ft. Crook Rd. South, Bellevue NE. 68005-2969
  1507. Phone: (402) 291-8300        FAX: (402) 291-4362
  1508. I never receive credit for anything I write! (I'm an Ohnemus. 8-)
  1509. 
  1510. 
  1511. Received: by charon.cwi.nl with SMTP; Sat, 7 Dec 1991 12:18:55 +0100
  1512. Received: by voorn.cwi.nl with SMTP; Sat, 7 Dec 1991 11:18:54 GMT
  1513. Message-Id: <9112071118.AA01357@voorn.cwi.nl>
  1514. To: python-list@cwi.nl
  1515. Subject: Re: Last message ( Scope & globals & binding ) 
  1516. In-Reply-To: Your message of "Fri, 06 Dec 91 14:31:14 MET."
  1517.              <9112061931.AA04791@aemsun.med.Virginia.EDU> 
  1518. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1519. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  1520. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  1521. Date: Sat, 07 Dec 91 12:18:53 +0100
  1522. Sender: Guido.van.Rossum@cwi.nl
  1523.  
  1524. "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU> writes:
  1525. >That was probably an incorrect statement to say that globals are
  1526. >READONLY.  The problems is not with globals but with assignment.
  1527. (etc.)
  1528.  
  1529. You are figuring out the obscure parts of Python at an amazing
  1530. speed...  You actually discovered something that I can't explain!
  1531. Indeed the documentation isn't very clear about globals except for
  1532. that one passage in misc/CLASSES.
  1533.  
  1534. The *real* *deep* reason why there is no global assignment is that
  1535. there is no static symbol table listing all names of known local and
  1536. global variables (really "name bindings" in the standard jargon that
  1537. I'm trying to establish for discussing these issues).  If the
  1538. interpreter encounters a statement "x = 1" it has no idea whether
  1539. there is a global x this might refer to, so the assignment is always
  1540. made local.  If there is *already* a global by this name it is
  1541. possible to find out (by just looking it up in the global name space),
  1542. but this would still prohibit creating new globals from within a
  1543. function.  I also thought that local variables would be in the
  1544. majority, and disliked the possibility that using a local variable
  1545. like "i" would accidentally clash with a global also named "i".
  1546. Therefore I decided that all variable assignments were to be local.
  1547.  
  1548. Variable references don't face the same problems, so they use a
  1549. local-scope-first rule; read-only acces to globals is essential
  1550. because (most) functions and imported modules are global name bindings.
  1551.  
  1552. The code generator (which didn't exist in very early versions of
  1553. Python) adds a little subtlety: it actually builds a list of local
  1554. variables by finding all name binding statements, and then generates
  1555. code that uses this fact throughout the function, distinguishing
  1556. between local and global lookups.  (The reason for this, BTW, is to
  1557. speed up global name lookups so that they don't first have to miss in
  1558. the local name set.)  This explains your incK() errors.  I was
  1559. surprised that this didn't break your earlier decN() example;
  1560. apparently the code generator generates different code, that uses the
  1561. old local-before-global rule when the global scope is the user's
  1562. workspace.  (Another way to trigger this is to write from
  1563. <some-module> import *; here the compiler can't figure out what the
  1564. local names will be.  But I think it's a bug here.)
  1565.  
  1566. You're right that a way of faking global variables is to lock them all
  1567. up in a class instance, and there are several demo programs that use
  1568. this trick (e.g., mclock).  Object-oriented design can usually avoid
  1569. the need for global variables in the first place.
  1570.  
  1571. But I still think that for simple programs the global variable
  1572. assignment has the merit of simplicity and relative elegance, and
  1573. actually I am thinking about introducing a 'global' statement with
  1574. which you can declare that names you are using in a function are
  1575. actually references to globals.  This statement would be placed
  1576. inside the function body that uses the globals.  Python's main source
  1577. of inspiration, ABC, has a similar statement (SHARE), and I think I
  1578. saw it in one of its competitors (Tcl) as well.  (What's Perl's
  1579. attitude towards locals and globals?  I forgot...)
  1580.  
  1581. BTW, the user's workspace is not super-global.  (Module 'builtin',
  1582. containing the built-in functions and exceptions, is.)  If you really
  1583. need to access the user's workspace from a module, import __main__.
  1584.  
  1585. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1586. "Well I'm a plumber.  I can't act"
  1587. 
  1588. 
  1589. Received: by charon.cwi.nl with SMTP; Sat, 7 Dec 1991 12:43:43 +0100
  1590. Received: by voorn.cwi.nl with SMTP; Sat, 7 Dec 1991 11:43:42 GMT
  1591. Message-Id: <9112071143.AA01381@voorn.cwi.nl>
  1592. To: python-list@cwi.nl
  1593. Subject: Re: passing/catching a variable number of args in python 
  1594. In-Reply-To: Your message of "Fri, 06 Dec 91 10:19:04 MET."
  1595.              <9112061619.AA01564@void.ncsa.uiuc.edu> 
  1596. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1597. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  1598. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  1599. Date: Sat, 07 Dec 91 12:43:41 +0100
  1600. Sender: Guido.van.Rossum@cwi.nl
  1601.  
  1602. >You could support calling f(x) by using a separate kind of function call,
  1603. >analogous to Lisp apply.  f(x) as compared with f(1, 2, 3) really does
  1604. >a different thing internally, and it was confusing to me as a
  1605. >new user to discover that these were intended to have the same result.
  1606.  
  1607. Erm, I don't understand what you mean by "really does a different
  1608. thing internally".  In my mind they do very different things.  In the
  1609. second case f is called with the expression-list "1, 2, 3" as
  1610. argument, which has the same value as x (which was assigned "1, 2, 3"
  1611. a line earlier).  Maybe you can explain what you felt again?
  1612.  
  1613. I believe that the equivalence is actually quite useful, and used
  1614. often enough that a special "apply" function would cripple the
  1615. language.  Take, for instance, a simple "plot" function that takes a
  1616. point as argument.  I can either write plot(x, y) or plot(p), assuming
  1617. p is a tuple of "x, y" coordinates, and I don't have to know whether
  1618. the function was declared with one (point) or with two (coordinate)
  1619. arguments.  Using your proposal (if I understand it well), I would
  1620. have to write f(p[0], p[1]) if it was declared as f(x, y), or I would
  1621. have to call it as f((x, y)) if it was declared as f(p).  Currently,
  1622. in Python, there is an informal rule that says "placing extra
  1623. parentheses never hurts".
  1624.  
  1625. Of course, all this makes Python's argument passing *very* different
  1626. from other languages (except ABC), which may be the reason why so many
  1627. people have difficulties with it -- either initially, or eternally.
  1628. The eternally case worries me most, if it exists, but the initially
  1629. case is also a reason to worry, since it makes Python harder to learn,
  1630. whicl easy-to-learn is one of its big advantages.
  1631.  
  1632. *** Question to the general audience: is this serious enough to
  1633. warrant changing the language (and potentially lots of existing code)?
  1634.  
  1635. >Maybe you want to consider calling f with arguments being the elements
  1636. >of the tuple x using the syntax "f x".  Then "f (1, 2, 3)" is really
  1637. >the same kind of function call as "f x".  This may be inconsistent with other
  1638. >aspects of the Python syntax; I dont have my grammar handy.
  1639.  
  1640. This was used in ABC, but it would be inconsistent in Python (assuming
  1641. no other syntax changes).  For example, it would render x[y] ambiguous:
  1642. is it a subscription of list/table x with index/key y, or is it a call
  1643. of function x with argument [y] (a list containing one element, y)?
  1644. Also, function calls without arguments would be impossible -- just "f"
  1645. doesn't call the function f but yields the function object f.  (This
  1646. isn't a problem in ABC since ABC has no function variables.)
  1647.  
  1648. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1649. "It's funny, isn't it?  How your best friend can just blow up like that?
  1650. I mean, you wouldn't think it was medically possible, would you?"
  1651. 
  1652. 
  1653. To: python-list@cwi.nl
  1654. Subject: Why print is a built-in statement
  1655. From: Guido van Rossum <guido@cwi.nl>
  1656. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  1657. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  1658. Date: Sat, 07 Dec 91 15:00:00 +0100
  1659. --------
  1660. Steven Majewski mentioned one issue that I didn't address yet: why
  1661. "print" is built-in in Python.  I'd like to explain this.
  1662.  
  1663. One of the reasons is that a simple print statement is useful for
  1664. simple programs.  Being a statement it doesn't need parentheses around
  1665. its argument list, which saves some typing.
  1666.  
  1667. Another reason is that it treats arguments separated by commas
  1668. differently than a single argument consisting of a tuple:
  1669.  
  1670.     print 'x =', x
  1671.  
  1672. might print
  1673.  
  1674.     x = 3.14
  1675.  
  1676. while
  1677.  
  1678.     t = 'x =', x
  1679.     print t
  1680.  
  1681. would print
  1682.  
  1683.     ('x =', 3.14)
  1684.  
  1685. (This has to do with the function argument discussion, by the way.)
  1686.  
  1687. The third reason has to do with the final linefeed written by print:
  1688. again, for simple programs, it's desirable that the simplest form of
  1689. print statement prints a complete line; but there also must be a way
  1690. to print partial lines.  Using special syntax (a trailing comma) to
  1691. specify the alternate behavior seemed preferable over having two
  1692. functions like Pascal's write and writeln (especially since it would
  1693. have meant two reserved words given the choice for non-function
  1694. syntax).
  1695.  
  1696. Writing it down makes it all sound like feeble excuses for a real
  1697. solution; but everything else I could think of had other
  1698. disadvantages.  Perhaps printing is one of these things that are
  1699. important enough to warrant more than one way to do things, even in
  1700. Python...
  1701.  
  1702. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1703. "*Nobody* expects the Spanish Inquisition"
  1704. 
  1705. 
  1706. Replied: Sun, 08 Dec 91 01:44:03 +0100
  1707. Replied: python-list@cwi.nl
  1708. Received: by charon.cwi.nl with SMTP; Sun, 8 Dec 1991 01:09:10 +0100
  1709. Received: from juno (juno.ericsson.se) by mailgate.ericsson.se (4.1/SMI-4.1-MAILGATE1.7)
  1710.     id AA07412; Sun, 8 Dec 91 01:09:05 +0100
  1711. Received: from juno21.ericsson.se by juno (4.1/SMI-4.1-LME1.6)
  1712.     id AA07694; Sun, 8 Dec 91 01:09:01 +0100
  1713. Received: by juno21.ericsson.se (4.1/client-1.3)
  1714.     id AA16478; Sun, 8 Dec 91 01:08:59 +0100
  1715. Date: Sun, 8 Dec 91 01:08:59 +0100
  1716. From: Mats Lidell TM/JUF 91753 <etxmsll@juno.ericsson.se>
  1717. Message-Id: <9112080008.AA16478@juno21.ericsson.se>
  1718. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1719. Cc: python-list@cwi.nl
  1720. Subject: Re: WHY PYTHON? 
  1721. References: <9112041742.AA01741@aemsun.med.Virginia.EDU>
  1722.     <9112061037.AA06793@voorn.cwi.nl>
  1723.  
  1724. >>>>> "Guido" == Guido van Rossum <Guido.van.Rossum@cwi.nl> writes:
  1725.  
  1726.   Guido> Using built-in classes as base classes for user-defined
  1727.   Guido> classes poses several problems, most of which lie in the
  1728.   Guido> realm of implementation.  Several other OO languages can live
  1729.   Guido> without this (C++, Modula-3), so I hope Python can as well.
  1730.  
  1731. The difficulties in the implementation doesn't mean much to the user
  1732. :-)
  1733.  
  1734. One promising thing with OO is that you can keep the name space small.
  1735. If you could build new types from the builtin the chance of
  1736. preserving the same name for similar operations would be greater.
  1737.  
  1738. A promising thing with small languages is that you don't have to learn
  1739. a lot of special purpose mumbo jumbo. This seems to be a good thing
  1740. for a script language since you'r not likely to use it everyday. But
  1741. the language also needs to have enough power so you'll easily express
  1742. what you want.  The OO-things can give you more specialized powerful
  1743. objects which might still be used in the same way as builtin objects.
  1744. If that's possible you have won a part of the battle of complexity.
  1745. Overloading builtin operations can be viewed in the same light. If
  1746. adding two objects make sense then maybe "+" is a good way to express
  1747. that.
  1748.  
  1749. These things are contradictory but I'll suggest keeping the smallness
  1750. in favor for adding more power with special constructs. Treating the
  1751. builtin types just as user defined wouldn't it make the language
  1752. smaller and easier to comprehend?
  1753.  
  1754. Another convenient thing for a script language would be to have
  1755. unbound precision integer arithmetic.
  1756.  
  1757. Yours
  1758. %% Mats Lidell 
  1759. etxmsll@juno.ericsson.se
  1760. "A man about to speak the truth should keep one foot in the stirrup."
  1761. -- Old Mongolian Saying
  1762. 
  1763. 
  1764. Received: by charon.cwi.nl with SMTP; Sun, 8 Dec 1991 01:44:03 +0100
  1765. Received: by voorn.cwi.nl with SMTP; Sun, 8 Dec 1991 00:44:02 GMT
  1766. Message-Id: <9112080044.AA02205@voorn.cwi.nl>
  1767. To: python-list@cwi.nl
  1768. Subject: Re: WHY PYTHON? 
  1769. In-Reply-To: Your message of "Sun, 08 Dec 91 01:08:59 MET."
  1770.              <9112080008.AA16478@juno21.ericsson.se> 
  1771. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1772. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  1773. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  1774. Date: Sun, 08 Dec 91 01:44:02 +0100
  1775. Sender: Guido.van.Rossum@cwi.nl
  1776.  
  1777. Mats Lidell writes:
  1778. >Another convenient thing for a script language would be to have
  1779. >unbound precision integer arithmetic.
  1780.  
  1781. Python has this, just append 'L' to your numeric constants.  It isn't
  1782. default because it is slower than ordinary integers, and I like to
  1783. keep Python reasonably efficient whenever possible.  (If you look real
  1784. careful, the language is full of places where speed was a design
  1785. criterion, even if it was rarely the only one.)
  1786.  
  1787. BTW, Python is not just a script language!  If all you need is
  1788. scripts, Tcl would be a better choice.  Python is a real programming
  1789. language, but concise enough to use for ad-hoc programming such as
  1790. scripts.  When you write your scripts in Python, you don't have to
  1791. recode them in a real language when they get larger.  Python is really
  1792. sitting halfway between script languages like sh and compiled language
  1793. like C.
  1794.  
  1795. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  1796. "Wenn ist das Nunnstueck git und Slotermeyer?"
  1797. "Ja! Beierhund das oder die flipperwaldt gersput."
  1798. 
  1799. 
  1800. Received: by charon.cwi.nl with SMTP; Sun, 8 Dec 1991 17:16:03 +0100
  1801. Received: by schelvis.cwi.nl with SMTP; Sun, 8 Dec 91 17:16:02 +0100
  1802. Message-Id: <9112081616.AA27158@schelvis.cwi.nl>
  1803. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1804. Cc: python-list@cwi.nl
  1805. Subject: Re: passing/catching a variable number of args in python 
  1806. In-Reply-To: Message by Guido van Rossum <Guido.van.Rossum@cwi.nl> ,
  1807.          Sat, 07 Dec 91 12:43:41 +0100 , <9112071143.AA01381@voorn.cwi.nl> 
  1808. Organisation: AMOEBA project, CWI, Kruislaan 413, Amsterdam
  1809. Phone: +31 20 5924098(work), +31 20 6160335(home)
  1810. X-Last-Band-Seen: Maroon town, Boter Ska en Eieren (Melkweg, 8-12)
  1811. X-Mini-Review: Party-ska!
  1812. Date: Sun, 08 Dec 91 17:16:01 +0100
  1813. From: Jack.Jansen@cwi.nl
  1814.  
  1815. The only things that took me some time to work out (with Guido's help)
  1816. with the parameter passing were the fact the the first argument of a
  1817. method was funny, so you have to use constructs like
  1818. def func(self,(foo,bar))
  1819. in some places. The other thing was the funny difference between
  1820. zero-, one- and many-argument functions (although this might be more
  1821. due to the handling of 1-sized tuples than anything else).
  1822. --
  1823. Jack Jansen        | In Holland things are serious, but never hopeless.
  1824. Jack.Jansen@cwi.nl | In Ireland things are hopeless, but never serious.
  1825. uunet!cwi.nl!jack   G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  1826. 
  1827. 
  1828. Replied: Mon, 09 Dec 91 10:54:55 +0100
  1829. Replied: python-list@cwi.nl
  1830. Received: by charon.cwi.nl with SMTP; Sun, 8 Dec 1991 23:46:27 +0100
  1831. Received: from juno (juno.ericsson.se) by mailgate.ericsson.se (4.1/SMI-4.1-MAILGATE1.7)
  1832.     id AA09610; Sun, 8 Dec 91 23:46:26 +0100
  1833. Received: from juno21.ericsson.se by juno (4.1/SMI-4.1-LME1.6)
  1834.     id AA00772; Sun, 8 Dec 91 23:46:22 +0100
  1835. Received: by juno21.ericsson.se (4.1/client-1.3)
  1836.     id AA22744; Sun, 8 Dec 91 23:46:20 +0100
  1837. Date: Sun, 8 Dec 91 23:46:20 +0100
  1838. From: Mats Lidell TM/JUF 91753 <etxmsll@juno.ericsson.se>
  1839. Message-Id: <9112082246.AA22744@juno21.ericsson.se>
  1840. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  1841. Cc: python-list@cwi.nl
  1842. Subject: Re: WHY PYTHON? 
  1843. References: <9112080008.AA16478@juno21.ericsson.se>
  1844.     <9112080044.AA02205@voorn.cwi.nl>
  1845.  
  1846. I said:
  1847. >Another convenient thing for a script language would be to have
  1848. >unbound precision integer arithmetic.
  1849.  
  1850. Guido said:
  1851.   Guido> Python has this, just append 'L' to your numeric constants.
  1852.  
  1853. Hup... I better get me a newer version of python! Sorry :-)
  1854.  
  1855. Since Python is the first real OO prototyping language I have come
  1856. across I must ask the following:
  1857.  
  1858. In an OO prototyping language, OOPL (?), the natural language to
  1859. connect to wouldn't be C but C++. A OOPL object could then be a thin
  1860. wrapper around the C++ object and of course you would like to be able
  1861. to make C++ code access OOPL objects as well.  Does anybody know of
  1862. some work in this direction? (Is it to weird to consider or is it in
  1863. Python already? :-))
  1864.  
  1865. Yours
  1866. %% Mats Lidell 
  1867. etxmsll@juno.ericsson.se
  1868. "Only a mediocre person is always at his best."
  1869. -- W. Somerset Maugham
  1870. 
  1871. 
  1872. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 09:00:35 +0100
  1873. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa12787;
  1874.           9 Dec 91 3:00 EST
  1875. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  1876.     id AA10178; Mon, 9 Dec 91 02:51:48 EST
  1877. Date: Mon, 9 Dec 91 02:51:48 EST
  1878. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  1879. Message-Id: <9112090751.AA10178@aemsun.med.Virginia.EDU>
  1880. To: Guido.van.Rossum@cwi.nl
  1881. Subject: Re: passing/catching a variable number of args in python
  1882. Cc: python-list@cwi.nl
  1883.  
  1884.  
  1885. I think this argument passing business is something I will want to 
  1886. mull over a while before giving you my considered opinion. 
  1887.  
  1888. BUT, my unconsidered opinion, right now, is that I approve of you
  1889. compromise. I don't think having two cases ( zero or one arg ) to 
  1890. deal with is too big a trade considering the flexibility you get 
  1891. with the tuple equivalence. 
  1892.  
  1893. I *don't* think breaking code should be a major consideration right now.
  1894. ( But then I am the proud author of exactly ONE peice of python code ;-) 
  1895. One reason for bringing up these issues is that you are at release 0.92 
  1896. and might as well have all the syntactic shake-outs BEFORE you get to 
  1897. 1.0 . At that point you should consider the major feature cast in stone,
  1898. but we will forgive you up till then. 
  1899.  
  1900. I *didn't* thin the logic of the zero-or-one/tuple-equivalence passing
  1901. mechanism was difficult to figure out, and your answer was more or less
  1902. what I expected, but I thought it would be a good topic for discussion. 
  1903.  
  1904. I think zero-or-one is only important in one direction, i.e. the typical
  1905. case is a procedure that expects ONE arg and is called with NONE. 
  1906. How about making argument mismatches a non-error.
  1907. Where there are too few dummy args, they get packed as a tuple in the last
  1908. arg ( I believe that IS the current action ? ) 
  1909. Where there are too few dummy args, the value 'None" becomes the arg. value.
  1910. ( I don't know how "None" is represented, so I don't know if that is 
  1911. practical. ) 
  1912.  
  1913. But if there is no clear winning compromise, I can live with things as they 
  1914. are. But, perhaps the exception namespace should be a little finer so we 
  1915. can just catch and only catch some "common" error ( like no arg when one 
  1916. expected! ) 
  1917.  
  1918. For that, I propose that the semantics be modified so that exception tuples
  1919. are effectively "flattened". This will allow a hierarchy in the exception
  1920. namespace ( less or more specific ). e.g. 
  1921.   SyntaxError = ( several different types of syntax error ) 
  1922.   TypeError = ( several different types of type error ) 
  1923.  
  1924.   try: 
  1925.      ...
  1926.   except ( SyntaxError, aSpecificTypeError ): 
  1927.  
  1928.   and the above is effectively the same as 
  1929.   except ( SyntaxErr-1, SyntaxErr-2, ... SyntaxErr-N, aSpecificTypeError ):
  1930.  
  1931. Exception signal mechanism's in VMS (for example) and ( I think ) in some
  1932. other languages that support exceptions, catch ALL of the appropriate 
  1933. signals, and give the user-exception processing code the responsibility of
  1934. filtering them. There is a "resignal" mechanism is VMS to pass the 
  1935. exception down to the next level of processing. 
  1936. This would be equivalent to python code checking the exception argument to
  1937. see if it was the one it was expecting, and if not, raising that exception
  1938. again, so that some outer code could have a chance of catching it. ( Or of
  1939. giving an error message - too general exception classes may have the 
  1940. effect of masking errors that the user didn't anticipate. That is REALLY
  1941. what I'm concerned about! ) 
  1942.  
  1943.  
  1944. Forgive me for not trying out and including some better examples and for 
  1945. being a bit too verbose for my own good. I'm writing this at home, 
  1946. ( without the Python manual at my side ) and editing on the modem 
  1947. can be a chore. 
  1948.  
  1949. If it wasn't 2AM, I would edit this down to two separate messages: 
  1950. argument passing & exceptions. 
  1951.  
  1952. - Steve
  1953.  
  1954. 
  1955. 
  1956. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 09:56:38 +0100
  1957. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa14509;
  1958.           9 Dec 91 3:56 EST
  1959. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  1960.     id AA10196; Mon, 9 Dec 91 03:48:03 EST
  1961. Date: Mon, 9 Dec 91 03:48:03 EST
  1962. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  1963. Message-Id: <9112090848.AA10196@aemsun.med.Virginia.EDU>
  1964. To: Guido.van.Rossum@cwi.nl
  1965. Subject: Re: Last message ( Scope & globals & binding )
  1966. Cc: python-list@cwi.nl
  1967.  
  1968. > From guido@cwi.nl Sat Dec  7 06:10:51 1991
  1969. > You are figuring out the obscure parts of Python at an amazing
  1970. > speed...  You actually discovered something that I can't explain!
  1971. > Indeed the documentation isn't very clear about globals except for
  1972. > that one passage in misc/CLASSES.
  1973. >
  1974. > ...
  1975. >
  1976. >      I also thought that local variables would be in the
  1977. > majority, and disliked the possibility that using a local variable
  1978. > like "i" would accidentally clash with a global also named "i".
  1979. > Therefore I decided that all variable assignments were to be local.
  1980. >
  1981. > ... 
  1982. >
  1983. > You're right that a way of faking global variables is to lock them all
  1984. > up in a class instance, and there are several demo programs that use
  1985. > this trick (e.g., mclock).  Object-oriented design can usually avoid
  1986. > the need for global variables in the first place.
  1987. > But I still think that for simple programs the global variable
  1988. > assignment has the merit of simplicity and relative elegance, and
  1989. > actually I am thinking about introducing a 'global' statement with
  1990. > which you can declare that names you are using in a function are
  1991. > actually references to globals.  This statement would be placed
  1992. > inside the function body that uses the globals.  Python's main source
  1993. > of inspiration, ABC, has a similar statement (SHARE), and I think I
  1994. > saw it in one of its competitors (Tcl) as well.  (What's Perl's
  1995. > attitude towards locals and globals?  I forgot...)
  1996.  
  1997. I agree that for a language that will be used for quick programs
  1998. ( and quick fixes/kludges ) globals are probably essential. 
  1999.  
  2000. The problem wasn't hard to find. ( Or maybe I should say the problem
  2001. wasn't hard to GENERATE - I did have a little trouble FINDING it. ) 
  2002. I was just throwing in one of those quick kludges and adding a global
  2003. variable was quicker than rethinking the module interfaces. 
  2004.  
  2005. However, the process of fixing it ( my program ) left me a bit ambivalent.
  2006. I an definitely not a proponent of "straight-jacket" program languages
  2007. that make it impossible to do many things in order to keep you from 
  2008. doing ONE BAD thing. But I'm not against a language that somwhow has 
  2009. features that encourage you to write GOOD code. 
  2010.  
  2011. The first fix was that I wrote my globals with "class Global(): pass" 
  2012. null classes. However, once I got the original to work, I was drawn
  2013. to try to OO it a bit more. 
  2014. I had globals: initial_depth, max_depth and local current_depth, plus
  2015. function dir_depth ( just counted the number of "/" in a pathname ).
  2016. I now have a half-asses OO version that was just bad enough to show 
  2017. me the RIGHT way to do it. So I found trying to do without globals 
  2018. a good learning experience. But probably not one I want to experience
  2019. when I'm in a big hurry to "get the damn thing working!" 
  2020.  
  2021. Anyway: 
  2022.  "global a = 0"
  2023.  "global b,c " 
  2024. Can easily be the syntactic shorthand for:
  2025.  Class __Global__():
  2026.     pass
  2027.  __global__ = __Global__()
  2028.  __global__.a = 0
  2029.  __global__.b =  
  2030.  __global__.c = 
  2031.  
  2032.  Global to the current module of course.
  2033.  But access to module.a would be equivalent to module.__global__.a 
  2034.  [ I am here assuming an addition to the symbol table search path. ]
  2035.  
  2036.  [ BTW Guido - have you ever looked at FORTH dictionaries ? 
  2037.    The syntax of alternate namespaces is awkward in FORTH, but
  2038.    the dictionaries are tree structured, with the search going 
  2039.    backward from the local leaves to the root ( with side trips 
  2040.    programmable ) so the use of the dictionary search order is 
  2041.    the primary way of managing the namespace. ]
  2042.  
  2043.  
  2044.  What to do with b,c - that's the rub. Ideally, we would like to 
  2045.  catch the error of access of uninitialized variables. i.e. using 
  2046.  the global value 'b' before an assignment to b should be signaled.
  2047.  Is there an easy way for it to be know to be a global name and yet
  2048.  be unbound ? 
  2049.  
  2050.  - Steve 
  2051.  
  2052. ( Again: sorry about the lack of discipline due to the late hour and 
  2053.   a slow modem at home. I meant to indicate that I agreed with the 
  2054.   comment I included above: preponderence of local ref. over global 
  2055.  and of global read ref. over global write ref. , etc. ) 
  2056.  
  2057. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  2058.  Steven D. Majewski        University of Virginia Physiology Dept.
  2059.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  2060.  Voice: (804)-982-0831        1600 Jefferson Park Avenue
  2061.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  2062. 
  2063. 
  2064. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 10:54:57 +0100
  2065. Received: by voorn.cwi.nl with SMTP; Mon, 9 Dec 1991 09:54:54 GMT
  2066. Message-Id: <9112090954.AA04220@voorn.cwi.nl>
  2067. To: python-list@cwi.nl
  2068. Subject: Re: WHY PYTHON? 
  2069. In-Reply-To: Your message of "Sun, 08 Dec 91 23:46:20 MET."
  2070.              <9112082246.AA22744@juno21.ericsson.se> 
  2071. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  2072. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2073. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2074. Date: Mon, 09 Dec 91 10:54:53 +0100
  2075. Sender: Guido.van.Rossum@cwi.nl
  2076.  
  2077. Mats Lidell writes:
  2078. >In an OO prototyping language, OOPL (?), the natural language to
  2079. >connect to wouldn't be C but C++. A OOPL object could then be a thin
  2080. >wrapper around the C++ object and of course you would like to be able
  2081. >to make C++ code access OOPL objects as well.
  2082.  
  2083. If all OO languages were the more or less equivalent except for minor
  2084. syntactic issues, this would be a reqsonable assumption.  However, as
  2085. it is, this would make the OO model of the wrapper basically the same
  2086. as that of the language it is wrapping.  I don't think you would want
  2087. Python's OO mechanisms too tightly bound to those of C++.
  2088.  
  2089. While the same reasoning could be applied to Python's reliance on C
  2090. for its non-OO features, I believe that at least the features from C
  2091. that I have borrowed for Python are much less controversial.
  2092.  
  2093. But the true reason why Python isn't based on C++ is of course
  2094. availability -- requiring C++ would initially restrict Python's
  2095. popularity mostly to the C++ community, for very practical reasons
  2096. like the availability of C++ compilers (and there is a lot of
  2097. incompatibility in that world as well!).
  2098.  
  2099. >Does anybody know of
  2100. >some work in this direction? (Is it to weird to consider or is it in
  2101. >Python already? :-))
  2102.  
  2103. There's no reason why some extension modules of Python couldn't be
  2104. written using C++, but of course this isn't the same as what you proposed.
  2105.  
  2106. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  2107. "That was never five minutes just now!"
  2108. "I'm afraid it was."
  2109. 
  2110. 
  2111. Replied: Mon, 09 Dec 91 18:53:05 +0100
  2112. Replied: python-list@cwi.nl
  2113. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 18:14:27 +0100
  2114. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA22928
  2115.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Mon, 9 Dec 91 11:09:13 -0600
  2116. Return-Path: <liberte@ncsa.uiuc.edu>
  2117. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2118.     id AA04145; Mon, 9 Dec 91 11:07:28 CST
  2119. Date: Mon, 9 Dec 91 11:07:28 CST
  2120. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2121. Message-Id: <9112091707.AA04145@void.ncsa.uiuc.edu>
  2122. To: Guido.van.Rossum@cwi.nl, python-list@cwi.nl
  2123. Subject: Re: passing/catching a variable number of args in python
  2124.  
  2125. I dont think optional or rest args (in Lisp terminology) would work
  2126. well in Python, at least not with the current mechanism of redistributing
  2127. actual arguments to fit formal arguments.  This brings up the issue of
  2128. ambiguous situations.  What if function f takes three arguments, a, b, c,
  2129. and is given two arguments (1, 2) and (3, 4)?  Do a and b get 1 and 2
  2130. leaving c with (3, 4), or does a get (1, 2) while b and c get 3 and 4? 
  2131. Whichever way it works, what if the user wants to do it the other way?
  2132.  
  2133.     >f(x) as compared with f(1, 2, 3) really does
  2134.     >a different thing internally, and it was confusing to me as a
  2135.     >new user to discover that these were intended to have the same result.
  2136.  
  2137.     Erm, I don't understand what you mean by "really does a different
  2138.     thing internally".  
  2139.  
  2140. Well, the user is supposed to not be concerned about whether separate
  2141. arguments are required, and so the two calls are semantically equivalent
  2142. since they result in the same thing, but to do the "same thing" in each
  2143. case, they have to do different things internally.
  2144.  
  2145.     Using your proposal (if I understand it well), I would
  2146.     have to write f(p[0], p[1]) if it was declared as f(x, y), or I would
  2147.     have to call it as f((x, y)) if it was declared as f(p).  Currently,
  2148.     in Python, there is an informal rule that says "placing extra
  2149.     parentheses never hurts".
  2150.  
  2151. I don't think calling f((x, y)) would be as annoying as f(p[0], p[1]),
  2152. so people would tend to declare the function with f(p) if it is meaningful
  2153. to collect x and y into one argument, and then if the function writer
  2154. wanted to break out the parts of p, the assignment x, y = p does the job.
  2155. Perhaps the same ambiguity I mentioned above exists in assignments..?
  2156.  
  2157. Alternatively, the function could be declared f((x, y)).  Analogously, the
  2158. assignment above should be done like (x, y) = p.  ML and probably
  2159. other languages have this kind of pattern-matching/argument-parsing
  2160. mechanism developed to a fine art.
  2161.  
  2162.     *** Question to the general audience: is this serious enough to
  2163.     warrant changing the language (and potentially lots of existing code)?
  2164.  
  2165. I think you need to think more about the ambiguous cases as well as user
  2166. confusion.
  2167.  
  2168.     >Maybe you want to consider calling f with arguments being the elements
  2169.     >of the tuple x using the syntax "f x".  Then "f (1, 2, 3)" is really
  2170.     >the same kind of function call as "f x".
  2171.  
  2172.     This was used in ABC, but it would be inconsistent in Python (assuming
  2173.     no other syntax changes).  For example, it would render x[y] ambiguous:
  2174.  
  2175. How about if x is a function object, then x[y] does a function call
  2176. with list [y] as argument.  Or if x is a list or table, then x[y] does the
  2177. subscription with index/key y.  You couldn't tell until run time which
  2178. it would be since the type of x is not declared, but the ambiguity would
  2179. be resolved.
  2180.  
  2181.     Also, function calls without arguments would be impossible 
  2182.     
  2183. Yes this is still a problem.  I prefer the ML-like pattern matching in
  2184. any case.
  2185.  
  2186.  
  2187. Dan LaLiberte
  2188. National Center for Supercomputing Applications
  2189.  
  2190. liberte@ncsa.uiuc.edu
  2191. (Join the League for Programming Freedom: league@prep.ai.mit.edu)
  2192. 
  2193. 
  2194. Replied: Mon, 09 Dec 91 19:14:23 +0100
  2195. Replied: python-list@cwi.nl
  2196. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 18:25:01 +0100
  2197. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA23797
  2198.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Mon, 9 Dec 91 11:19:53 -0600
  2199. Return-Path: <liberte@ncsa.uiuc.edu>
  2200. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2201.     id AA04476; Mon, 9 Dec 91 11:18:59 CST
  2202. Date: Mon, 9 Dec 91 11:18:59 CST
  2203. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2204. Message-Id: <9112091718.AA04476@void.ncsa.uiuc.edu>
  2205. To: Guido.van.Rossum@cwi.nl, python-list@cwi.nl
  2206. Subject: Other Python syntax changes
  2207.  
  2208. Other issues I have been considering from time to time...
  2209.  
  2210. I'd like to see optional static typing, perhaps of the ML kind. 
  2211. Although lack of type declarations is useful during prototyping, I like
  2212. to use type declarations even then to have the compiler make sure that
  2213. I am being consistent.  Maybe declarations should take the form of
  2214. general assertions so they may be used throughout code for more general
  2215. consistency checks.
  2216.  
  2217. Also, I would like to see an optional alternate block structuring
  2218. syntax that doesnt rely on indenting and line termination.  Russell has
  2219. this, I believe.  There are problems with portability of source code
  2220. between editors with different tabsize defaults.  Also, it inhibits
  2221. automatic code generation since indenting must be accounted for.  
  2222.  
  2223. Related to this, I like Lisp's representation of functions with Lisp
  2224. data structures, so you can look at them, and modify them.  Whatever
  2225. this representation is for Python, one should be able to enter a function
  2226. using constructors for the same data structures (e.g. (def, func, (args) ...)).
  2227.  
  2228. Dan LaLiberte
  2229. National Center for Supercomputing Applications
  2230.  
  2231. liberte@ncsa.uiuc.edu
  2232. (Join the League for Programming Freedom: league@prep.ai.mit.edu)
  2233. 
  2234. 
  2235. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 18:53:05 +0100
  2236. Received: by voorn.cwi.nl with SMTP; Mon, 9 Dec 1991 17:53:05 GMT
  2237. Message-Id: <9112091753.AA05130@voorn.cwi.nl>
  2238. To: python-list@cwi.nl
  2239. Subject: Re: passing/catching a variable number of args in python 
  2240. In-Reply-To: Your message of "Mon, 09 Dec 91 11:07:28 MET."
  2241.              <9112091707.AA04145@void.ncsa.uiuc.edu> 
  2242. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  2243. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2244. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2245. Date: Mon, 09 Dec 91 18:53:04 +0100
  2246. Sender: Guido.van.Rossum@cwi.nl
  2247.  
  2248. Daniel LaLiberte writes:
  2249. >I dont think optional or rest args (in Lisp terminology) would work
  2250. >well in Python, at least not with the current mechanism of redistributing
  2251. >actual arguments to fit formal arguments.
  2252.  
  2253. There is no such mechanism.  Functions either have no argument or
  2254. they have one argument.  The syntax for one argument allows a
  2255. comma-separated list of expressions, which builds a tuple; it is this
  2256. tuple value that becomes the *single* argument to the function.  When
  2257. the function is actually declared with multiple arguments, it expects
  2258. a tuple and unpacks this into the respective arguments, just like an
  2259. unpacking assignment.
  2260.  
  2261. >This brings up the issue of
  2262. >ambiguous situations.  What if function f takes three arguments, a, b, c,
  2263. >and is given two arguments (1, 2) and (3, 4)?  Do a and b get 1 and 2
  2264. >leaving c with (3, 4), or does a get (1, 2) while b and c get 3 and 4? 
  2265. >Whichever way it works, what if the user wants to do it the other way?
  2266.  
  2267. There is no ambiguity.  This is a run-time error (detected in the
  2268. called function), similar in nature to the error in assignments like
  2269. "a, b = 1, 2, 3".
  2270.  
  2271. (This is Daniel:)
  2272. >    >f(x) as compared with f(1, 2, 3) really does
  2273. >    >a different thing internally, and it was confusing to me as a
  2274. >    >new user to discover that these were intended to have the same result.
  2275. (my reply:)
  2276. >    Erm, I don't understand what you mean by "really does a different
  2277. >    thing internally".  
  2278. (Daniel again:)
  2279. >Well, the user is supposed to not be concerned about whether separate
  2280. >arguments are required, and so the two calls are semantically equivalent
  2281. >since they result in the same thing, but to do the "same thing" in each
  2282. >case, they have to do different things internally.
  2283.  
  2284. I still maintain nothing is done differently.  Maybe my explanation
  2285. above (1-or-1 argumet) explains your confusion?
  2286.  
  2287. >Alternatively, the function could be declared f((x, y)).  Analogously, the
  2288. >assignment above should be done like (x, y) = p.  ML and probably
  2289. >other languages have this kind of pattern-matching/argument-parsing
  2290. >mechanism developed to a fine art.
  2291.  
  2292. Isn't Python's mechanism very similar (although probably implemented
  2293. totally different), except that extra parentheses don't count?  After
  2294. all, you can define a function as follows
  2295.  
  2296.     def f(a, (b, c, d), (e, (f, g))): ...
  2297.  
  2298. and call it in either of the following ways:
  2299.  
  2300.     f(a, (b, c, d), (e, (f, g)))
  2301.     bcd = b, c, d
  2302.     fg = f, g
  2303.     f(a, bcd, (e, fg))
  2304.     efg = e, fg
  2305.     f(a, bcd, efg)
  2306.     abcdefg = a, bcd, efg
  2307.     f(abcdefg)
  2308.  
  2309. (But I admit I don't know even a little bit of ML.)
  2310.  
  2311. >How about if x is a function object, then x[y] does a function call
  2312. >with list [y] as argument.  Or if x is a list or table, then x[y] does the
  2313. >subscription with index/key y.
  2314.  
  2315. I have the feeling that this is something which should be unambigous
  2316. at compile time (at least in a computer language -- in natural
  2317. languages this kind of things happens all the time).  [If you still
  2318. disagree I'm willing to go into an argument off-line; I have a feeling
  2319. this is different from using '+' for strings and numbers, but it takes
  2320. too long to explain why I think that is so.]
  2321.  
  2322. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  2323. "I'm sorry, is this a five-minute argument, or the full half-hour?"
  2324. 
  2325. 
  2326. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 19:14:24 +0100
  2327. Received: by voorn.cwi.nl with SMTP; Mon, 9 Dec 1991 18:14:23 GMT
  2328. Message-Id: <9112091814.AA05160@voorn.cwi.nl>
  2329. To: python-list@cwi.nl
  2330. Subject: Re: Other Python syntax changes 
  2331. In-Reply-To: Your message of "Mon, 09 Dec 91 11:18:59 MET."
  2332.              <9112091718.AA04476@void.ncsa.uiuc.edu> 
  2333. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  2334. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2335. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2336. Date: Mon, 09 Dec 91 19:14:22 +0100
  2337. Sender: Guido.van.Rossum@cwi.nl
  2338.  
  2339. Daniel LaLiberte writes:
  2340.  
  2341. >I'd like to see optional static typing, perhaps of the ML kind. 
  2342. >Although lack of type declarations is useful during prototyping, I like
  2343. >to use type declarations even then to have the compiler make sure that
  2344. >I am being consistent.  Maybe declarations should take the form of
  2345. >general assertions so they may be used throughout code for more general
  2346. >consistency checks.
  2347.  
  2348. Wouldn't you like it if the compiler could figure out that you were
  2349. being inconsistent even without type declarations?  This is the kind
  2350. of static checking present in ABC, and I'd like to introduce it in
  2351. Python as well -- eventually.  I know there are problems implementing
  2352. this in Python (for ABC it's solved), but I know *I* would like the
  2353. resulting system, if it were ever built, better than one which
  2354. required me to supply type declarations that are obvious from the
  2355. context...
  2356.  
  2357. >Also, I would like to see an optional alternate block structuring
  2358. >syntax that doesnt rely on indenting and line termination.  Russell has
  2359. >this, I believe.  There are problems with portability of source code
  2360. >between editors with different tabsize defaults.
  2361.  
  2362. I don't care much for this personally (even though about half of all
  2363. Python development happens in an editor with 4-space tab stops for
  2364. default), but I've heard the complaint before.  Maybe someone on the
  2365. list could write up a solid proposal?
  2366.  
  2367. >Also, it inhibits
  2368. >automatic code generation since indenting must be accounted for.
  2369.  
  2370. (I presume you mean generating Python statements on the fly for exec()
  2371. etc.)  In fact it is very easy to increase the indentation of a piece
  2372. of code by just inserting a tab at the beginning of each line.
  2373.  
  2374. >Related to this, I like Lisp's representation of functions with Lisp
  2375. >data structures, so you can look at them, and modify them.  Whatever
  2376. >this representation is for Python, one should be able to enter a function
  2377. >using constructors for the same data structures (e.g. (def, func, (args) ...))
  2378.  
  2379. You can inspect the generated (virtual machine) code of a function or
  2380. module, but you can't change it since it is represented as immutable
  2381. objects.  The standard modules "dis.py" and "tb.py" use this --
  2382. "dis" disassembles the generated code, which is useful for debugging
  2383. the code generator, and "tb" allows inspection of the stack trace
  2384. after a program has died.  (I should really document this one...)
  2385.  
  2386. But I'd like to hide this representation from most users, so it can be
  2387. changed if the needs of the parser, code generator and static tester
  2388. change.  In Lisp, the representation of the parse tree is pretty
  2389. obvious, given its lack of syntax; in Python, however, the parser has
  2390. a lot of freedom.  I don't believe there is in general much need for
  2391. self-modifying programs of the kind that Lisp allows; most practical
  2392. applications of it (such as on-the-fly code generation) are possible
  2393. in Python using eval() and exec() without the need to ever peek or
  2394. poke around in function objects.
  2395.  
  2396. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  2397. "It's probably pining for the fiords"
  2398. 
  2399. 
  2400. Replied: Tue, 10 Dec 91 11:38:41 +0100
  2401. Replied: "liberte@ncsa.uiuc.edu (Daniel LaLiberte) +python-list"
  2402. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 20:17:26 +0100
  2403. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA02055
  2404.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Mon, 9 Dec 91 13:12:15 -0600
  2405. Return-Path: <liberte@ncsa.uiuc.edu>
  2406. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2407.     id AA05698; Mon, 9 Dec 91 13:11:20 CST
  2408. Date: Mon, 9 Dec 91 13:11:20 CST
  2409. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2410. Message-Id: <9112091911.AA05698@void.ncsa.uiuc.edu>
  2411. To: Guido.van.Rossum@cwi.nl, python-list@cwi.nl
  2412. Subject: Re: passing/catching a variable number of args in python
  2413.  
  2414.     Daniel LaLiberte writes:
  2415.     >I dont think optional or rest args (in Lisp terminology) would work
  2416.  
  2417.     There is no such mechanism.  
  2418.  
  2419. I was referring (without the explicit reference) to someone else's suggestion
  2420. to consider optional and rest args.  I rather like them too, and adding
  2421. them to current Python might be a problem.
  2422.  
  2423.     > [calling function of two arguments with 3 arguments...]
  2424.  
  2425.     There is no ambiguity.  This is a run-time error (detected in the
  2426.     called function), similar in nature to the error in assignments like
  2427.     "a, b = 1, 2, 3".
  2428.  
  2429. So in this situation, one is required to put the explicit parens in
  2430. (i.e. f((1, 2), 3)), but as a special case, you don't want to require
  2431. explicit parens in f(1, 2) when passed to a function declared f(p).  
  2432. I can appreciate the motivation for this, but the inconsistency bothers
  2433. me more.
  2434.  
  2435.     >Alternatively, the function could be declared f((x, y)).  
  2436.  
  2437.     Isn't Python's mechanism very similar (although probably implemented
  2438.     totally different), except that extra parentheses don't count?  After
  2439.     all, you can define a function as follows
  2440.  
  2441.         def f(a, (b, c, d), (e, (f, g))): ...
  2442.  
  2443. Oh, well I guess I forgot that Python can do this (can you tell I
  2444. am not an active user?).  In that case, I don't see any point in making
  2445. parens optional at the top level of an argument list, except for the
  2446. small convenience it affords sometimes.  Look at it this way: if the
  2447. user is grouping things in a tuple, the elements should have more
  2448. relationship to one another than just the fact that they will be passed
  2449. as a collective argument to a function.  And if doing away with this
  2450. "problem" leads to a better solution to the no argument/one argument
  2451. problem, I am all in favor of this code breaking change to Python syntax.
  2452.  
  2453. dan
  2454. 
  2455. 
  2456. Replied: Tue, 10 Dec 91 11:28:35 +0100
  2457. Replied: "liberte@ncsa.uiuc.edu (Daniel LaLiberte) +audio"
  2458. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 20:23:45 +0100
  2459. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA02533
  2460.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Mon, 9 Dec 91 13:20:26 -0600
  2461. Return-Path: <liberte@ncsa.uiuc.edu>
  2462. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2463.     id AA05844; Mon, 9 Dec 91 13:19:29 CST
  2464. Date: Mon, 9 Dec 91 13:19:29 CST
  2465. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2466. Message-Id: <9112091919.AA05844@void.ncsa.uiuc.edu>
  2467. To: Guido.van.Rossum@cwi.nl
  2468. Subject: Re: Other Python syntax changes
  2469.  
  2470. If you can do automatic static type checking without declarations, great!
  2471. But I suggested the general assertion mechanism as perhaps the more
  2472. valuable enhancement to python.  How about if a logical expression
  2473. appears as a statement, then the expression must evaluate to true.
  2474. If it fails, then an exception is raised.  This would avoid having
  2475. to introduce new syntax, but "=" would be a problem.  So add an
  2476. assert: expression statement.
  2477.  
  2478. dan
  2479. 
  2480. 
  2481. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 22:45:06 +0100
  2482. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa25043;
  2483.           9 Dec 91 16:44 EST
  2484. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  2485.     id AA10771; Mon, 9 Dec 91 16:36:23 EST
  2486. Date: Mon, 9 Dec 91 16:36:23 EST
  2487. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  2488. Message-Id: <9112092136.AA10771@aemsun.med.Virginia.EDU>
  2489. To: liberte@ncsa.uiuc.edu
  2490. Subject: Re: passing/catching a variable number of args in python
  2491. Cc: python-list@cwi.nl
  2492.  
  2493. My notion was that: 
  2494.  
  2495. a,b = 1,2,3 
  2496.  
  2497. should unpack as:
  2498.  a = 1 ; b = ( 2, 3 )
  2499.  
  2500. and:
  2501.  
  2502. a,b,c = 1,2 
  2503.  
  2504. as:
  2505.  a = 1; b = 2; c = None 
  2506.  
  2507. I'm not sure that if it came to a vote, I would vote for my own
  2508. proposal - just thinking out loud at the moment. 
  2509.  
  2510. I see the case as to whether or not an arg mismatch should raise an 
  2511. error/exception as similar to the pro-con type checking argument: 
  2512.  If python is intended for quickie programming, then it should 
  2513. *REQUIRE* the minimum in type checking & other strictness. Given 
  2514. any statement it should try to do something sensible ( within some
  2515. sensible bounds ). I believe you WILL want type checking or some sort
  2516. of consistency checking in larger programs/modules. In that case 
  2517. there should be a "Python-lint" or some such program external to the
  2518. compiler/interpreter. 
  2519.  
  2520. But I won't kill for that belief (yet :-)
  2521. ( Also, as I said before, I don't know how the value 'None' is 
  2522.  represented. ) 
  2523.  
  2524. Maybe Guido can give us some more details on consistency checking
  2525. in ABC. ( Or maybe I'll get off my duff and read the ABC doc's myself.)
  2526.  
  2527. - Steve
  2528. 
  2529. 
  2530. Replied: Tue, 10 Dec 91 01:15:21 +0100
  2531. Replied: python-list@cwi.nl
  2532. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 23:18:31 +0100
  2533. Received: from PSUVM.PSU.EDU (MAILER@PSUVM) by HEARNVAX.nic.SURFnet.nl with
  2534.  PMDF#10216; Mon, 9 Dec 1991 23:19 MET
  2535. Received: by PSUVM (Mailer R2.08) id 5479; Mon, 09 Dec 91 17:16:30 EST
  2536. Date: Mon, 9 Dec 91 17:16 EST
  2537. From: CXC19@PSUVM.PSU.EDU
  2538. Subject: Python for MS-DOS?
  2539. To: python-list@cwi.nl
  2540. Message-Id: <062AC4A6A0002464@HEARNVAX.nic.SURFnet.nl>
  2541. X-Envelope-To: python-list@CWI.NL
  2542.  
  2543. I hope this is an appropriate forum for my question.  Where can
  2544. I get a copy of Python for my PC?  I have looked at a Canadian
  2545. site which was mentioned before but didn't find it there.
  2546.  
  2547. Thanks,
  2548.  
  2549. Carl Cotner
  2550. cxc19@psuvm.psu.edu
  2551. 
  2552. 
  2553. Received: by charon.cwi.nl with SMTP; Mon, 9 Dec 1991 23:27:48 +0100
  2554. Received: from juno (juno.ericsson.se) by mailgate.ericsson.se (4.1/SMI-4.1-MAILGATE1.7)
  2555.     id AA16467; Mon, 9 Dec 91 23:27:42 +0100
  2556. Received: from juno21.ericsson.se by juno (4.1/SMI-4.1-LME1.6)
  2557.     id AA03273; Mon, 9 Dec 91 23:27:39 +0100
  2558. Received: by juno21.ericsson.se (4.1/client-1.3)
  2559.     id AA09438; Mon, 9 Dec 91 23:27:36 +0100
  2560. Date: Mon, 9 Dec 91 23:27:36 +0100
  2561. From: Mats Lidell TM/JUF 91753 <etxmsll@juno.ericsson.se>
  2562. Message-Id: <9112092227.AA09438@juno21.ericsson.se>
  2563. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  2564. Cc: python-list@cwi.nl
  2565. Subject: Re: WHY PYTHON? 
  2566. References: <9112082246.AA22744@juno21.ericsson.se>
  2567.     <9112090954.AA04220@voorn.cwi.nl>
  2568.  
  2569. Guido writes:
  2570.  
  2571.   Guido> If all OO languages were the more or less equivalent except
  2572.   Guido> for minor syntactic issues, this would be a reqsonable
  2573.   Guido> assumption.  However, as it is, this would make the OO model
  2574.   Guido> of the wrapper basically the same as that of the language it
  2575.   Guido> is wrapping.  I don't think you would want Python's OO
  2576.   Guido> mechanisms too tightly bound to those of C++.
  2577.  
  2578. Definitely not!! But I'm not sure how tight this bound has to be. If
  2579. you want to support everything you are probably in for trouble but
  2580. normally even C interfaces are very restricted in just allowing
  2581. passing of basic types between the sides (Sorry for not checking this
  2582. with Python :-).
  2583.  
  2584.   Guido> But the true reason why Python isn't based on C++ is of
  2585.   Guido> course availability.
  2586.  
  2587. Agreed!
  2588.  
  2589.   Guido> There's no reason why some extension modules of Python
  2590.   Guido> couldn't be written using C++, but of course this isn't the
  2591.   Guido> same as what you proposed.
  2592.  
  2593. Hmm... Well that's interesting (and maybe obvious). I guess I'd better
  2594. check how this works with Python before boring you to death on this
  2595. subject since this mailing list really is about Python :-)
  2596.  
  2597. %% Mats
  2598. "It is easier to fight for principles than to live up to them."
  2599. -- Alfred Adler
  2600. 
  2601. 
  2602. Received: by charon.cwi.nl with SMTP; Tue, 10 Dec 1991 01:15:21 +0100
  2603. Received: by voorn.cwi.nl with SMTP; Tue, 10 Dec 1991 00:15:20 GMT
  2604. Message-Id: <9112100015.AA06015@voorn.cwi.nl>
  2605. To: python-list@cwi.nl
  2606. Subject: Re: Python for MS-DOS? 
  2607. In-Reply-To: Your message of "Mon, 09 Dec 91 17:16:00 MET."
  2608.              <062AC4A6A0002464@HEARNVAX.nic.SURFnet.nl> 
  2609. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  2610. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2611. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2612. Date: Tue, 10 Dec 91 01:15:19 +0100
  2613. Sender: Guido.van.Rossum@cwi.nl
  2614.  
  2615. Carl Cotner writes:
  2616. >I hope this is an appropriate forum for my question.  Where can
  2617. >I get a copy of Python for my PC?  I have looked at a Canadian
  2618. >site which was mentioned before but didn't find it there.
  2619.  
  2620. I don't have a PC with a C compiler, so I can't help you personally.
  2621. However...
  2622.  
  2623. Long ago Mark Anacker posted patches for MS-DOS to alt.sources that
  2624. worked with version 0.9.1.
  2625.  
  2626. I have lost contact with Mark, but have incorporated his patches in
  2627. 0.9.2, except for systematic changes to include file names to make
  2628. them short enough (I don't know if these were really necessary
  2629. anyway).  Of course, new changes, especially to the POSIX module,
  2630. could cause new trouble, but I believe that porting Python to MS-DOS
  2631. should be fairly simple, given a good C compiler (Turbo C or Microsoft
  2632. C) and somebody who's done this kind of work before.
  2633.  
  2634. Perhaps some appropriately equipped volunteer could try to do a port?
  2635. I'd be happy to assist via e-mail or telephone calls, and I will take
  2636. care of making the final binary avaiable by anon. ftp to the world.
  2637.  
  2638. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  2639. "I'm sorry, is this a five-minute argument, or the full half-hour?"
  2640. 
  2641. 
  2642. To: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2643. Subject: Re: Other Python syntax changes 
  2644. In-reply-to: Your message of "Mon, 09 Dec 91 13:19:29 MET."
  2645.              <9112091919.AA05844@void.ncsa.uiuc.edu> 
  2646. From: Guido van Rossum <guido@cwi.nl>
  2647. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2648. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2649. Date: Tue, 10 Dec 91 11:28:39 +0100
  2650. Sender: guido
  2651.  
  2652. Assertions can easily be done without changes to the language, e.g. by
  2653. writing a function like this:
  2654.  
  2655.     def assert(test):
  2656.         if not test: raise <some exception>
  2657.  
  2658. I suppose this can be made into a standard module, complete with its
  2659. own exception, if there's enough demand (on the other hand, it's
  2660. trivial enough that interested parties can write it themselves).
  2661.  
  2662. --Guido
  2663. 
  2664. 
  2665. To: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2666. Subject: Re: passing/catching a variable number of args in python 
  2667. In-reply-to: Your message of "Mon, 09 Dec 91 13:11:20 MET."
  2668.              <9112091911.AA05698@void.ncsa.uiuc.edu> 
  2669. From: Guido van Rossum <guido@cwi.nl>
  2670. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2671. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2672. Date: Tue, 10 Dec 91 11:38:44 +0100
  2673. Sender: guido
  2674.  
  2675. Sorry Dan, I am getting a little tired of this discussion of tuples
  2676. and arguments.  The current use of tuples in Python is quite
  2677. consistent (although totally different from superficially similar
  2678. mechanisms in other languages), and changing only argument passing is
  2679. not really possible without breaking lots of things in subtle ways.
  2680. This would just be too major a change to the language to be worth it.
  2681. After all *one* of the constraints to language changes is that I must
  2682. find the time to implement them...
  2683.  
  2684. --Guido
  2685. 
  2686. 
  2687. Replied: Wed, 11 Dec 91 23:46:50 +0100
  2688. Replied: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  2689. Received: by charon.cwi.nl with SMTP; Wed, 11 Dec 1991 21:55:39 +0100
  2690. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa20019;
  2691.           11 Dec 91 15:55 EST
  2692. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  2693.     id AA10936; Tue, 10 Dec 91 12:12:44 EST
  2694. Date: Tue, 10 Dec 91 12:12:44 EST
  2695. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  2696. Message-Id: <9112101712.AA10936@aemsun.med.Virginia.EDU>
  2697. To: Guido.van.Rossum@cwi.nl
  2698. Subject: Re: Python for MS-DOS?
  2699.  
  2700. I will probably try porting python to MS-DOS ( unless someone else 
  2701. does it first - I won't try to predict when I will be able to get 
  2702. around to it. ) 
  2703. Any offhand guesses as to the memory requirements ? 
  2704. Will it be usable in >640K or will I need a memory extender ? 
  2705. I have ported a few things to DOS, but I'm not that adept in 
  2706. portability requirements - I would think the goal for dos would 
  2707. be to have an executable that could be shipped instead of the 
  2708. sources. ( And some of the dos memory extenders will not allow 
  2709. this without royalty agreements ! ) 
  2710.  
  2711. - Steve
  2712. 
  2713. 
  2714. Replied: Tue, 10 Dec 91 23:48:04 +0100
  2715. Replied: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2716. Received: by charon.cwi.nl with SMTP; Tue, 10 Dec 1991 19:04:18 +0100
  2717. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA14980
  2718.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Tue, 10 Dec 91 12:01:40 -0600
  2719. Return-Path: <liberte@ncsa.uiuc.edu>
  2720. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2721.     id AA18008; Tue, 10 Dec 91 12:00:47 CST
  2722. Date: Tue, 10 Dec 91 12:00:47 CST
  2723. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2724. Message-Id: <9112101800.AA18008@void.ncsa.uiuc.edu>
  2725. To: Guido.van.Rossum@cwi.nl
  2726. Subject: Re: Other Python syntax changes
  2727.  
  2728.     Assertions can easily be done without changes to the language, e.g. by
  2729.     writing a function like this:
  2730.  
  2731.         def assert(test):
  2732.             if not test: raise <some exception>
  2733.  
  2734. Although using a simple assert function goes a long way, there is more
  2735. that an full-blown assertion mechanism should do for the programmer. 
  2736. e.g. often one wants to compare the previous value of an expression (when
  2737. a function was entered) with the current value.  Eiffel has a pretty
  2738. good assertion mechanism melded in with the class system, to constrain
  2739. the relationships between super classes and subclasses.  I dont think
  2740. you would want to go that far, but it has some good ideas.
  2741.  
  2742. The exception that is raised by a failed assertion should reflect the
  2743. caller of the assertion somehow, as well as the type of exception being
  2744. raised.  If the assert function can get at the caller by itself, then
  2745. all we need is a second parameter to indicate the exception type.
  2746.  
  2747. More daring folks (like myself) will want to compile out assertions
  2748. when they are satisfied that the code is "safe".  But assertions could
  2749. still be used as a control construct (when the exception is caught), so not
  2750. all assertions should be compiled out.
  2751.  
  2752. How could one do module or class invarients in Python?  These are
  2753. assertions that hold after any function in the module or class is
  2754. called.  One would want to declare the assertion only once and have
  2755. it called automatically after each function call (or whenever the
  2756. state of the module or class instance may be changed).  If there were a
  2757. hook on function calling or message sending, then a module or class
  2758. could just call the assertion in the hook function for that module or class.
  2759.  
  2760. An automatic type checker should be able to use assertions to assist
  2761. in type checking.
  2762.  
  2763. To do comparison with old values in current Python, one would have to
  2764. save the old value in a local variable explicitly, which is probably
  2765. more flexible and not too difficult.  Eiffel also has a notion of a
  2766. loop varient which is a non-negative integer expression that always
  2767. decreases in value after each loop.  This would be pretty messy to code
  2768. explicitly and probably wouldnt be used much in Python since many loops
  2769. are implicit or are already guarenteed to halt.
  2770.  
  2771. Well, enough for now.
  2772. dan
  2773. 
  2774. 
  2775. Replied: Tue, 10 Dec 91 23:36:31 +0100
  2776. Replied: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2777. Received: by charon.cwi.nl with SMTP; Tue, 10 Dec 1991 19:11:54 +0100
  2778. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA15251
  2779.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Tue, 10 Dec 91 12:09:17 -0600
  2780. Return-Path: <liberte@ncsa.uiuc.edu>
  2781. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2782.     id AA18037; Tue, 10 Dec 91 12:08:23 CST
  2783. Date: Tue, 10 Dec 91 12:08:23 CST
  2784. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2785. Message-Id: <9112101808.AA18037@void.ncsa.uiuc.edu>
  2786. To: Guido.van.Rossum@cwi.nl
  2787. Subject: Re: passing/catching a variable number of args in python
  2788.  
  2789.     Sorry Dan, I am getting a little tired of this discussion of tuples
  2790.     and arguments.  
  2791.  
  2792. Well, I've made my best pitch for the way I'd like to see things, so
  2793. there isnt much more to say.
  2794.  
  2795.     After all *one* of the constraints to language changes is that I must
  2796.     find the time to implement them...
  2797.  
  2798. Implementing this change to the "language" would only involve taking out
  2799. code to handle the special cases.  But if that breaks lots of code (a
  2800. significant part of the "language" no doubt), it would be better to fix
  2801. the code first to use the new convention as time permits and only then
  2802. change the language.
  2803.  
  2804. dan
  2805. 
  2806. 
  2807. To: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2808. Subject: Re: passing/catching a variable number of args in python 
  2809. In-reply-to: Your message of "Tue, 10 Dec 91 12:08:23 MET."
  2810.              <9112101808.AA18037@void.ncsa.uiuc.edu> 
  2811. From: Guido van Rossum <guido@cwi.nl>
  2812. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2813. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2814. Date: Tue, 10 Dec 91 23:36:29 +0100
  2815. Sender: guido
  2816.  
  2817. >Implementing this change to the "language" would only involve taking out
  2818. >code to handle the special cases.
  2819.  
  2820. Which special cases?  I am not being a nuisance here; I really don't
  2821. think there are any special cases.  Maybe you can be specific about
  2822. what you think are special cases.  (Since we are now discussing this
  2823. in private, you can take as much space as you need, if it makes your
  2824. point.)
  2825.  
  2826. >But if that breaks lots of code (a
  2827. >significant part of the "language" no doubt), it would be better to fix
  2828. >the code first to use the new convention as time permits and only then
  2829. >change the language.
  2830.  
  2831. What exactly is the new convention you are proposing?  That "a,b=1,2,3"
  2832. assign 1 to a and (2,3) to b?  I would be vehemently opposed to this,
  2833. as well as to its equivalent in argument passing.
  2834.  
  2835. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  2836. "Oh Bevis!  And I thought you were so rugged!"
  2837. 
  2838. 
  2839. To: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2840. Subject: Re: Other Python syntax changes 
  2841. In-reply-to: Your message of "Tue, 10 Dec 91 12:00:47 MET."
  2842.              <9112101800.AA18008@void.ncsa.uiuc.edu> 
  2843. From: Guido van Rossum <guido@cwi.nl>
  2844. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2845. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2846. Date: Tue, 10 Dec 91 23:48:02 +0100
  2847. Sender: guido
  2848.  
  2849. >The exception that is raised by a failed assertion should reflect the
  2850. >caller of the assertion somehow, as well as the type of exception being
  2851. >raised.  If the assert function can get at the caller by itself, then
  2852. >all we need is a second parameter to indicate the exception type.
  2853.  
  2854. I *think* there is a way of getting at the stack trace from within a
  2855. function.  There certainly is a way to do this from within an
  2856. exception handler -- the Python debugger uses this.
  2857.  
  2858. >More daring folks (like myself) will want to compile out assertions
  2859. >when they are satisfied that the code is "safe".  But assertions could
  2860. >still be used as a control construct (when the exception is caught), so not
  2861. >all assertions should be compiled out.
  2862.  
  2863. Well, there's "raise" for that, which is never compiled out.
  2864.  
  2865. >How could one do module or class invarients in Python?  These are
  2866. >assertions that hold after any function in the module or class is
  2867. >called.  One would want to declare the assertion only once and have
  2868. >it called automatically after each function call (or whenever the
  2869. >state of the module or class instance may be changed).  If there were a
  2870. >hook on function calling or message sending, then a module or class
  2871. >could just call the assertion in the hook function for that module or class.
  2872.  
  2873. Now there's an interesting idea.  It wouldn't be too hard to break
  2874. parts of the interpreter open and allow the user to specify "hooks"
  2875. that are called when various interesting things happen, such as
  2876. calling a function, returning from a function, or perhaps executing
  2877. any instruction.  This could easily be specified on a per-function
  2878. basis, and would make for a very general tracing and debugging
  2879. facility...
  2880.  
  2881. I'd like to define it in such a way that it can't be used (or at least
  2882. doesn't encourage use) to change the "local" semantics of Python, but
  2883. side effects of the kind that a trace or log facility must have would
  2884. be fine.
  2885.  
  2886. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  2887. "All right, it's a fair cop, but society is to blame."
  2888. 
  2889. 
  2890. Received: by charon.cwi.nl with SMTP; Wed, 11 Dec 1991 18:38:57 +0100
  2891. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA16463
  2892.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Wed, 11 Dec 91 11:34:55 -0600
  2893. Return-Path: <liberte@ncsa.uiuc.edu>
  2894. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  2895.     id AA26453; Wed, 11 Dec 91 11:34:00 CST
  2896. Date: Wed, 11 Dec 91 11:34:00 CST
  2897. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  2898. Message-Id: <9112111734.AA26453@void.ncsa.uiuc.edu>
  2899. To: Guido.van.Rossum@cwi.nl
  2900. Subject: Re: passing/catching a variable number of args in python
  2901.  
  2902.     >Implementing this change to the "language" would only involve taking
  2903.     >out code to handle the special cases.
  2904.  
  2905.     Which special cases?  
  2906.  
  2907. I guess I was only thinking of one special case: in argument passing, if
  2908. there is one formal argument in a function declaration, then no matter how
  2909. many actual arguments are given, they are all packaged up in a tuple for
  2910. the one formal argument. So for
  2911.  
  2912. def f(x) ...
  2913.  
  2914. The call f(1, 2) puts the tuple (1, 2) in x.
  2915. This case has some similarity with the following, but they are quite
  2916. different nevertheless.
  2917.  
  2918. def f(x, y) ...
  2919.  
  2920. f((1, 2), 3)   -- x is given (1, 2).
  2921.  
  2922. ----
  2923. Consider the following.
  2924.  
  2925. def f((x, y), z)
  2926.  
  2927. f((1, 2), 3, 4)  -- an error
  2928. f((1, 2), (3, 4))  -- so parens around 3, 4 is required
  2929. f((1, (2.1, 2.2)), (3, 4))  -- y gets (2.1, 2.2)  I have no problem here
  2930.  
  2931. def f((x), y)
  2932.  
  2933. f((1), 2)     -- x gets 1
  2934. f((1, 2), 3)  -- this I don't like.  should x get tuple (1, 2)?  I say no. 
  2935.         -- an error would be more consistent.  what does python do?
  2936.  
  2937.     What exactly is the new convention you are proposing?  That "a,b=1,2,3"
  2938.     assign 1 to a and (2,3) to b?  I would be vehemently opposed to this,
  2939.     as well as to its equivalent in argument passing.
  2940.  
  2941. I would be opposed too.  In fact, I am going the other direction.
  2942.  
  2943. a = 1, 2, 3 has the same problem described above
  2944. as def f(a) given f(1, 2, 3).
  2945.  
  2946. I propose that instead of a = 1, 2, 3, one must say a = (1, 2, 3)
  2947. and instead of def f(a) given f(1, 2, 3) one must say f((1, 2, 3)).
  2948. Or if one wishes to break out the components of a tuple in a function,
  2949. one could def f((x, y, z)) and be given the same f((1, 2, 3)) or just
  2950. f(xyz) where xyz = (1, 2, 3).  But def f(x, y, z) can only be given
  2951. three separate arguments, e.g. f(1, 2, 3).
  2952.  
  2953. The rule is that for a left hand side (or formal argument list),
  2954. there must be a one-to-one correspondence between names and values on
  2955. the right hand side (or actual argument list).  This same rule is applied
  2956. consistently recursively.
  2957.  
  2958. To implement this, you could still build an implicit tuple out of
  2959. actual arguments, but build the corresponding implicit tuple out of formal
  2960. arguments and do the matching on corresponding elements of the tuples,
  2961. recursively.  The same for assignment.
  2962.  
  2963. Now that I have presented it more clearly, I am more convinced myself that
  2964. this is the right way to go.  I am not sure how this proposal fits in with
  2965. zero argument functions and methods and (1,) style tuples, but I would
  2966. bet that doing away with this special case makes other things simpler.
  2967.  
  2968. Had you considered matching corresponding elements of lists too?
  2969. So def f([a, b]) could be given f([1, 2]).  
  2970.  
  2971. Some way of doing explicit optional and rest arguments could be added
  2972. given this new consistent convention.  So def f(a, b?, c*) might mean b is
  2973. optional and c gets all following arguments in a tuple.  f(1, 2, 3, 4)
  2974. puts (3, 4) in c.  But f(1, 2, 3) puts (3,) in c.  f(1) puts Novalue in b
  2975. and perhaps () in c.
  2976.  
  2977. dan
  2978. 
  2979. 
  2980. To: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  2981. Subject: Re: Python for MS-DOS? 
  2982. In-reply-to: Your message of "Tue, 10 Dec 91 12:12:44 MET."
  2983.              <9112101712.AA10936@aemsun.med.Virginia.EDU> 
  2984. From: Guido van Rossum <guido@cwi.nl>
  2985. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  2986. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  2987. Date: Wed, 11 Dec 91 23:46:48 +0100
  2988. Sender: guido
  2989.  
  2990. >I will probably try porting python to MS-DOS ( unless someone else 
  2991. >does it first - I won't try to predict when I will be able to get 
  2992. >around to it. )
  2993.  
  2994. Thanks, I haven't found any other volunteers except a guy in South
  2995. Africa who seems to have no porting experience at all.
  2996.  
  2997. >Any offhand guesses as to the memory requirements ?
  2998. >Will it be usable in >640K or will I need a memory extender ?
  2999.  
  3000. On the Mac the interpreter is about 200 kBytes and runs in 1 Meg,
  3001. although it runs out of memory parsing long modules such as testall.py
  3002. (which really should be split anyway).  This includes the STDWIN
  3003. module which doesn't apply on the PC.  I expect it will be at least
  3004. somewhat functional on the PC in 640 k.  I don't know anything about
  3005. memory expanders; they will have to be integrated with malloc (so the
  3006. program can allocate expanded memory using malloc) or you'd be in deep
  3007. trouble using one.
  3008.  
  3009. >I have ported a few things to DOS, but I'm not that adept in 
  3010. >portability requirements - I would think the goal for dos would 
  3011. >be to have an executable that could be shipped instead of the 
  3012. >sources.
  3013.  
  3014. That's my idea as well.
  3015.  
  3016. >( And some of the dos memory extenders will not allow 
  3017. >this without royalty agreements ! ) 
  3018.  
  3019. That would be out of the question :-(
  3020.  
  3021. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  3022. "It's funny, isn't it?  How your best friend can just blow up like that?
  3023. I mean, you wouldn't think it was medically possible, would you?"
  3024. 
  3025. 
  3026. To: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  3027. Subject: Re: passing/catching a variable number of args in python 
  3028. In-reply-to: Your message of "Wed, 11 Dec 91 11:34:00 MET."
  3029.              <9112111734.AA26453@void.ncsa.uiuc.edu> 
  3030. From: Guido van Rossum <guido@cwi.nl>
  3031. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  3032. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  3033. Date: Thu, 12 Dec 91 14:08:32 +0100
  3034. Sender: guido
  3035.  
  3036. You did a fine job: you've convinced me that there is a way to change
  3037. Python's argument passing without breaking the whole language, and
  3038. allowing several problems with arguments to be fixed.  I even think
  3039. that this can be done in an almost backward-compatible way (which is
  3040. important to myself, since I've got over 10,000 lines of Python code
  3041. lying around...).
  3042.  
  3043. Still no guarantees that I'll do it, but it sounds attractive since it
  3044. would solve two current problems: the exceptional position of
  3045. functions without arguments and the weird argument lists required for
  3046. methods.
  3047.  
  3048. I don't think it is necessary to change assignments at all.  The most
  3049. important new rule would be:
  3050.  
  3051. 1.    Formal and actual parameter lists are *always* seen as
  3052.     tuples: a single parameter is seen as a singleton tuple, an
  3053.     empty parameter list is seen as an empty tuple.
  3054.  
  3055. (It is important to realize that in Python the caller has no idea of
  3056. the callee's formal parameter list, so there must be an agreed upon
  3057. representation of actual parameter lists.)
  3058.  
  3059. Two extra rules provide coercion between tuples and single parameters;
  3060. these are only invoked (in the callee, when receiving the argument
  3061. list) if the number of actual and formal parameters don't match:
  3062.  
  3063. 2.    If there is exactly one actual parameter and 0 or 2 or more
  3064.     formal parameters, the actual parameter must be a tuple with
  3065.     as many items, and its items are distributed over the formal
  3066.     parameters.
  3067.  
  3068. 3.    If there is exactly one formal parameter and 0 or 2 or more
  3069.     actual parameters, the formal paramater receives the tuple of
  3070.     actual parameters.
  3071.  
  3072. Rule 2 makes it possible to do:
  3073.  
  3074.     def f(a, b, c): ...
  3075.  
  3076.     x = 1, 2, 3
  3077.     f(x)        # equivalent to f(1, 2, 3)
  3078.  
  3079. which is important if you have a function and an argument list that
  3080. you want to save somewhere (perhaps pass it to another module) so it
  3081. can be called later.  The old system had an anomaly that such a module
  3082. couldn't both handle function calls with and without arguments; the
  3083. new system lets you pass "f, ()" in this case.
  3084.  
  3085. Rule 3 handles the reverse case:
  3086.  
  3087.     def f(x): ...
  3088.  
  3089.     f(1, 2, 3)    # equivalent to f((1, 2, 3))
  3090.  
  3091. which makes it possible to let the function detect the number of
  3092. arguments.  This still requires the callee to handle a single actual
  3093. parameter special (since then the formal parameter won't be a tuple).
  3094. Also this mechanism makes it impossible to pass a variadic function
  3095. one argument which is a tuple -- I'm not sure if this is a problem,
  3096. but if it is, we are now able to define special syntax in the formal
  3097. parameter list to handle variable length argument lists special (e.g.,
  3098. &rest).
  3099.  
  3100. One problem left to solve is the mapping from method calls to function
  3101. calls.  We can now get rid of the silly convention that a method with
  3102. more than one argument must enclose all arguments except "self" in
  3103. extra parentheses:
  3104.  
  3105.     # old style
  3106.     class C():
  3107.         def method(self, (x, y, z)): ...
  3108.  
  3109. can now become
  3110.  
  3111.     # new style
  3112.     class C:
  3113.         def method(self, x, y, z): ...
  3114.  
  3115. I haven't thought about a backward compatibility hack here -- perhaps
  3116. I'll solve that with a script...
  3117.  
  3118. Now I have to get back to your mail.
  3119.  
  3120. >def f((x), y)
  3121. >
  3122. >f((1), 2)     -- x gets 1
  3123. >f((1, 2), 3)  -- this I don't like.  should x get tuple (1, 2)?  I say no. 
  3124. >        -- an error would be more consistent.  what does python do?
  3125.  
  3126. Currently "def f((x), y)" is indistinguishable from "def f(x, y)",
  3127. just as "(1)+1" is indistinguishable from "1+1".  I'd like to keep
  3128. this rule in general.  My proposal above only makes the outermost
  3129. parentheses of function calls and declarations special.
  3130.  
  3131. >I propose that instead of a = 1, 2, 3, one must say a = (1, 2, 3)
  3132.  
  3133. I don't see why this requirement would improve the language.
  3134.  
  3135. >and instead of def f(a) given f(1, 2, 3) one must say f((1, 2, 3)).
  3136.  
  3137. Not with my proposal, because of rule 3.
  3138.  
  3139. >Had you considered matching corresponding elements of lists too?
  3140. >So def f([a, b]) could be given f([1, 2]).
  3141.  
  3142. Not too useful, since lists are mostly used when they may vary in
  3143. length.
  3144.  
  3145. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  3146. "That was never five minutes just now!"
  3147. "I'm afraid it was."
  3148. 
  3149. 
  3150. Received: by charon.cwi.nl with SMTP; Thu, 12 Dec 1991 18:05:46 +0100
  3151. Received: from yoyodyne.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA00115
  3152.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Thu, 12 Dec 91 11:04:03 -0600
  3153. Return-Path: <liberte@ncsa.uiuc.edu>
  3154. Received: by yoyodyne.ncsa.uiuc.edu (4.0/NCSA-4.1)
  3155.     id AA16424; Thu, 12 Dec 91 11:02:34 CST
  3156. Date: Thu, 12 Dec 91 11:02:34 CST
  3157. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  3158. Message-Id: <9112121702.AA16424@yoyodyne.ncsa.uiuc.edu>
  3159. To: Guido.van.Rossum@cwi.nl
  3160. Subject: Re: passing/catching a variable number of args in python
  3161.  
  3162.     You did a fine job: you've convinced me that there is a way to change
  3163.     Python's argument passing without breaking the whole language, and
  3164.     allowing several problems with arguments to be fixed.  
  3165.  
  3166. Well, I'm glad we persisted then.  But from your counter proposal, my
  3167. main contribution seems to be rule 1.  I would also, over time, do away
  3168. with rules 2 and 3, which seem to exist mostly for backward compatibility.
  3169. I would try to provide their unique features in some other way;
  3170. in Lisp, rule 2 would be replaced by using apply and rule 3 by &rest args.
  3171.     
  3172.     I don't think it is necessary to change assignments at all.  
  3173.     
  3174. No, it isn't necessary, just more consistent.  But given rules 2 and 3,
  3175. doing "a = 1, 2, 3" and "a, b, c = (1, 2, 3)" is consistent.
  3176.  
  3177.  
  3178.     Now I have to get back to your mail.
  3179.  
  3180.     >def f((x), y)
  3181.     >
  3182.     >f((1), 2)     -- x gets 1
  3183.     >f((1, 2), 3)  -- this I don't like.  should x get tuple (1, 2)?  I say no. 
  3184.     >        -- an error would be more consistent.  what does python do?
  3185.  
  3186.     Currently "def f((x), y)" is indistinguishable from "def f(x, y)",
  3187.     just as "(1)+1" is indistinguishable from "1+1".  I'd like to keep
  3188.     this rule in general.  My proposal above only makes the outermost
  3189.     parentheses of function calls and declarations special.
  3190.  
  3191. OK, how about def f((x, ), y) then?
  3192.  
  3193.     >Had you considered matching corresponding elements of lists too?
  3194.     >So def f([a, b]) could be given f([1, 2]).
  3195.  
  3196.     Not too useful, since lists are mostly used when they may vary in
  3197.     length.
  3198.  
  3199. Well, granted my suggestion above is not too useful by itself.
  3200. But if lists vary in length, then it would be more useful to have
  3201. a way of matching all remaining elements of a list.
  3202.  
  3203. Feel free to use any of my mail in a summary to the python list if
  3204. you wish.
  3205.  
  3206. dan
  3207. 
  3208. 
  3209. Received: by charon.cwi.nl with SMTP; Thu, 19 Dec 1991 05:26:02 +0100
  3210. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa24042;
  3211.           18 Dec 91 23:25 EST
  3212. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  3213.     id AA09202; Wed, 18 Dec 91 23:16:58 EST
  3214. Date: Wed, 18 Dec 91 23:16:58 EST
  3215. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  3216. Message-Id: <9112190416.AA09202@aemsun.med.Virginia.EDU>
  3217. To: python-list@cwi.nl
  3218. Subject: python moved/re-moved from wuarchive.wuslt.edu:/pub
  3219.  
  3220.  
  3221. >From Tim Peters / tim@ksr.com 
  3222. I hear that the python distribution is no longer in wuarchive.wuslt.edu:/pub
  3223.  
  3224. I currenly have a copy in uvaarpa.virginia.edu:/public_access/Python.tar.Z 
  3225.  
  3226. This is not the original distribution, but a superset, including 
  3227. Gnu-readline-library, STDWIN library & programs & python.beta.0.92 
  3228. including binaries for SPARC/SunOS4.1.1. 
  3229.  
  3230. It is *rather* large. 
  3231.  
  3232. I will probably put the original there also, if needed. 
  3233. Any other USA ftp sites ? 
  3234.  
  3235. - Steve Majewski
  3236. 
  3237. 
  3238. Received: by charon.cwi.nl with SMTP; Mon, 23 Dec 1991 19:19:36 +0100
  3239. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa13749;
  3240.           23 Dec 91 13:19 EST
  3241. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  3242.     id AA04380; Mon, 23 Dec 91 13:10:52 EST
  3243. Date: Mon, 23 Dec 91 13:10:52 EST
  3244. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  3245. Message-Id: <9112231810.AA04380@aemsun.med.Virginia.EDU>
  3246. To: python-list@cwi.nl
  3247. Subject: miscellaneous 
  3248.  
  3249. --------
  3250. >>>( 1 > 0.0 ) 
  3251. 1
  3252.  
  3253. - OK on my machine. 
  3254.   ( Tim: you must have re-built the binaries for your machine, or
  3255.     else it is stranger still... )
  3256. --------
  3257.  
  3258. Re: Fixed in 0.9.4 
  3259.  Is 0.9.4 ready and available yet ? 
  3260.  ( I will try to keep a current distribution on uvaarpa.virginia.edu 
  3261.    for U.S. - What is there now is my combined ( Readline & STDWIN ) 
  3262.    version of 0.9.2 with the one tiny #include patch for RS6000 
  3263.    and SPARC/SunOS 4.1.1 binaries. That was stuck there so that so 
  3264.    that some other people at UVA could grab it and install it on the
  3265.    SparcStations here quickly, but in the future, I'll try to keep 
  3266.    a "vanilla" copy of the latest release. )
  3267.  Can you ( Guido ) post a "changes" file to the list when available ? 
  3268.  
  3269. ---------
  3270.  
  3271.  Re: '==' replacing '=' for comparison
  3272.  
  3273.  Yes. That was on my list of minor complaints. I don't think comparison
  3274.  and assignment should have the same symbol. If changing also makes 
  3275.  the grammar/parser easier, then go ahead!  '==' for equality is 
  3276.  probably better that ':=' for assignment, as assignment is probably
  3277.  the more frequent operation. 
  3278.  
  3279. ---------
  3280.  
  3281. In General ( Re: scope & binding thread )
  3282.  
  3283.  I agree that the rules in python are not too difficult to understand. 
  3284.  They are just different from what one expects, but that is certainly
  3285.  no reason to change. ( In fact many of the things I like most about 
  3286.  python are outgrowths of it's object-oriented ( I mean in the object
  3287.  .vs. value sense ), name spaces, and assignment-is-name-binding. )
  3288.   
  3289.  I'm quite willing to learn the python way of doing things, as long as
  3290.  that is clearly spelled out. ( In fact, I've been holding off on 
  3291.  the "what python needs is ..." comments until I feel that I *DO* 
  3292.  grasp that notion more firmly. )
  3293.  
  3294.  What is needed is to merge some of the points discussed here into the
  3295.  users guide - a prominent part of the intro should explain what is 
  3296.  different about python - with a more in depth treatment in the Lang. 
  3297.  Reference Manual. 
  3298.  
  3299.  ( I'm sure you've got plenty of work to do already, Guido. I hope 
  3300.    that this discussion has been some help in clarifying the questions, 
  3301.    ( I never understand what *I* mean until I hear myself explain it :-)
  3302.    rather than merely drain of your time & resources. )
  3303.  
  3304. -----------
  3305.  
  3306.  ( A longer post on names & classes follows... )
  3307.  
  3308.  - Steve Majewski
  3309.  
  3310. 
  3311. 
  3312. Received: by charon.cwi.nl with SMTP; Mon, 23 Dec 1991 20:27:08 +0100
  3313. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa16514;
  3314.           23 Dec 91 14:27 EST
  3315. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  3316.     id AA04490; Mon, 23 Dec 91 14:18:27 EST
  3317. Date: Mon, 23 Dec 91 14:18:27 EST
  3318. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  3319. Message-Id: <9112231918.AA04490@aemsun.med.Virginia.EDU>
  3320. To: python-list@cwi.nl
  3321. Subject: Nameless objects have name spaces ( huzzah! hurray! :-) 
  3322.  
  3323.  
  3324. The further discussion of names & binding in python raises some 
  3325. dormant questions in my mind.
  3326.  
  3327. Guido's comment that in python: "an expression does not compute a 
  3328. value but yields an object" is, like "assignment is not the modification
  3329. of a storage cell, but the binding of a name to an object", a seminal
  3330. point. These two sentences should be somewhere prominent in the user
  3331. guide - in *BOLD* or underlined! 
  3332.  
  3333. It has some implications I haven't fully thought out yet. 
  3334. ( I *was* prepared to argue that, to exploit interactive terseness, 
  3335.   all python expressions should yield values ( like in C or ICON )
  3336.   and that side effect expressions ( like C: if ( ( n = read() ) == max ) ... )
  3337.   should be allowed, as well as op-assignment ( +=, etc. ). 
  3338.   Mainly because python loops often seem to require some "forethought" : 
  3339.   perhaps not a bad thing for a programmer to be forced into :-), but in
  3340.   interactive use, it can be an annoyance. 
  3341.   I am rethinking this position. )
  3342.  
  3343. It *DID* inspire me to discover that un-named objects also have a 
  3344. name space. 
  3345.  
  3346. (*example*):
  3347.  
  3348. >>> class Thing():
  3349. ...  pass
  3350. ...
  3351. >>> T = ( Thing(), Thing(), Thing() )
  3352. >>> T
  3353. (<instance object at 7af78>, <instance object at 83470>, <instance object at 83)
  3354. >>>
  3355. >>> type( T )
  3356. <type 'tuple'>
  3357. >>>
  3358. >>> T[0]
  3359. <instance object at 7af78>
  3360. >>>
  3361. >>> type( T[0] )
  3362. <type 'instance'>
  3363. >>>
  3364. >>> for x in T :
  3365. ...   x.L = []
  3366. ...   x.i = 0
  3367. ...
  3368. >>> T[0]
  3369. <instance object at 7af78>
  3370. >>> T[0].L
  3371. []
  3372. >>>
  3373. >>> T[0].i
  3374. 0
  3375. >>>
  3376.  
  3377. The application of this is that I would like to transform my tuple of 
  3378. tuples into a tuple of objects without cluttering up the namespace 
  3379. with what should be anonymous objects.
  3380. E.g. I have a list of tuples: 
  3381.  [ ( IdNumber, 'Na4-P2-O7', ( 'Na', 4, peak-count-for-Na ),
  3382.                ( 'P',  2, peak-count-for-P ) )  ... ]
  3383. ( Perhaps this structure can be cleaned up a bit in it's logical grouping,
  3384. but the point is: )
  3385. References like: x[0][2][2] get to be a bit opaque. But each item is 
  3386. a pair in  a larger list, and I don't want/need them each to be named.
  3387.  
  3388.  
  3389.  
  3390. This example, however, leads me back to one of my other problems/complaints
  3391. with python classes: 
  3392.  I does not seem possible ( to me: I may not understand this correctly )
  3393.  to initialize an object at it's creation. i.e. what I want to be able 
  3394.  to do is something like: 
  3395.  
  3396. class Thing( y ):
  3397.   Thing.x = y
  3398.   Thing.i = 0
  3399.  
  3400. or 
  3401.  
  3402. class Thing():
  3403.   def init( me ):
  3404.     me.L = []
  3405.     me.i = 0 
  3406.     return me
  3407.   Thing().init()
  3408.  
  3409. But I have been unable to find an acceptable syntax variation of the 
  3410. above that is accepted. 
  3411.  
  3412. BUT:
  3413. >>> Thing().L = []
  3414.  
  3415. Does work.
  3416. Though: 
  3417.  
  3418. >>> a = ( Thing().L = [] )
  3419. Unhandled exception: undefined name: L
  3420. Stack backtrace (innermost last):
  3421.   File "<stdin>", line 1
  3422. >>>
  3423.  
  3424. Obviously does not, since the parends make it look like comparison 
  3425. is intended. ( Will this work when "equals" becomes '==' ? )
  3426.  
  3427.  
  3428. Actually, while writing this note, I have been swapping between
  3429. emacs & python to try examples, and I finally figured out how to 
  3430. do what I want ( But I will go ahead and send this, instead of 
  3431. just saying "Nevermind", for discussion sake. ):
  3432.  
  3433. >>> class Thing():
  3434. ...   def init(me):
  3435. ...     me.L = []
  3436. ...     me.i = 0
  3437. ...     return me
  3438. ...
  3439. >>> T = ( Thing().init(), Thing().init() )
  3440. >>> T
  3441. (<instance object at 7add0>, <instance object at 836f0>)
  3442. >>> T[0]
  3443. <instance object at 7add0>
  3444. >>> T[0].i
  3445. 0
  3446. >>> T[0].L
  3447. []
  3448. >>>
  3449. >>> a = Thing().init()
  3450. >>> a
  3451. <instance object at 84908>
  3452. >>> a.i
  3453. 0
  3454. >>> a.L
  3455. []
  3456. >>>
  3457.  
  3458. I *still* think it would be nice if there was a way to MAKE classes 
  3459. become initialized when created  as part of their definition, 
  3460. rather that by explicitly calling an init() function. ( Is there 
  3461. a way that I'm missing ? ) But I can probably live with the above 
  3462. method. 
  3463.  
  3464. - Steve Majewski
  3465. 
  3466. 
  3467. Received: by charon.cwi.nl with SMTP; Mon, 23 Dec 1991 23:01:26 +0100
  3468. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa23518;
  3469.           23 Dec 91 17:01 EST
  3470. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  3471.     id AA04621; Mon, 23 Dec 91 16:52:42 EST
  3472. Date: Mon, 23 Dec 91 16:52:42 EST
  3473. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  3474. Message-Id: <9112232152.AA04621@aemsun.med.Virginia.EDU>
  3475. To: python-list@cwi.nl
  3476. Subject: disappearance of python
  3477.  
  3478. Not only is python.9.2 no longer in wuarchive.wustl.edu:/pub 
  3479. but I do not see it on uunet.uu.net:/tmp or 
  3480. gatekeeper.dec.com:/pub/misc/python 
  3481.  
  3482. And ftp.nluug.nl & mcsun.eu.net only seem to have v 9.1. 
  3483.  
  3484. Is there some insideous plot by Perl users to stamp out python ? 
  3485. ( I *have* spotted a live elvis.tar.Z, so anything is possible... :-) 
  3486.  
  3487. Merry Christmas to all, 
  3488.  - Steve
  3489. 
  3490. 
  3491. Received: by charon.cwi.nl with SMTP; Mon, 23 Dec 1991 23:26:46 +0100
  3492. Received: by paring.cwi.nl ; Mon, 23 Dec 91 23:26:44 +0100
  3493. Date: Mon, 23 Dec 91 23:26:44 +0100
  3494. From: Dik.Winter@cwi.nl
  3495. Message-Id: <9112232226.AA00341@paring.cwi.nl>
  3496. To: python-list@cwi.nl, sdm7g@aemsun.med.Virginia.EDU
  3497. Subject: Re:  disappearance of python
  3498.  
  3499.  > Not only is python.9.2 no longer in wuarchive.wustl.edu:/pub 
  3500.  > but I do not see it on uunet.uu.net:/tmp or 
  3501.  > gatekeeper.dec.com:/pub/misc/python 
  3502.  > 
  3503.  > And ftp.nluug.nl & mcsun.eu.net only seem to have v 9.1. 
  3504.  > 
  3505.  > Is there some insideous plot by Perl users to stamp out python ? 
  3506.  > ( I *have* spotted a live elvis.tar.Z, so anything is possible... :-) 
  3507. Perhaps.  For the time being at least it is available from:
  3508.     ftp.cwi.nl:/pub/python0.9.2beta.tar.Z
  3509. I do not know whether that is exactly the same version (Guido will know
  3510. that).
  3511.  > 
  3512.  > Merry Christmas to all, 
  3513. Same, dik
  3514. --
  3515. dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland
  3516. dik@cwi.nl
  3517. 
  3518. 
  3519. To: python-list@cwi.nl
  3520. Subject: Re: Steve Majewski's mail
  3521. From: Guido van Rossum <guido@cwi.nl>
  3522. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  3523. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  3524. Date: Tue, 24 Dec 91 12:02:43 +0100
  3525. Sender: guido
  3526.  
  3527. Steve Majewski replied on the list to a message not sent to the list;
  3528. I hope y'all list readers can figure out the issues from what he quoted.
  3529. I'll only reply to points that are still unclear.
  3530.  
  3531. > Is 0.9.4 ready and available yet ?
  3532.  
  3533. Good question.  I could either release what I currently have as
  3534. "0.9.4" or stick to it a little longer.  My main reason for not
  3535. releasing it is that I still (sigh) haven't found the time to update
  3536. all the documentation.  However a new release since 0.9.2beta is long
  3537. overdue so maybe I'll release it *without* up-to-date documentation.
  3538. I will post an announcement with a summary of changes on the list when
  3539. it's ready.  People who have a strong opinion about releasing or not
  3540. releasing should send mail to me privately so I can just tally the
  3541. votes.
  3542.  
  3543. > Re: '==' replacing '=' for comparison
  3544. >
  3545. > Yes. That was on my list of minor complaints. I don't think comparison
  3546. > and assignment should have the same symbol. If changing also makes 
  3547. > the grammar/parser easier, then go ahead!  '==' for equality is 
  3548. > probably better that ':=' for assignment, as assignment is probably
  3549. > the more frequent operation.
  3550.  
  3551. I agree, reluctantly (because of all the lines of library and example
  3552. code I will have to change), that this change should be made; but it
  3553. won't be in 0.9.4 if I release that soon.  Note that this still won't
  3554. give you assignments in expressions as side effects; I am against such
  3555. a feature even though it occasionally saves a line of code -- one of
  3556. Python's strong points is that it is usually very easy (and pretty!)
  3557. to read, and I feel that assignments in expressions would have a
  3558. strong negative effect on readability (and prettiness!).
  3559.  
  3560. > What is needed is to merge some of the points discussed here into the
  3561. > users guide - a prominent part of the intro should explain what is 
  3562. > different about python - with a more in depth treatment in the Lang. 
  3563. > Reference Manual.
  3564.  
  3565. The current "tutorial" tries to keep object-oriented features out of
  3566. sight for as long as possible, in order to explain other pertinent
  3567. features like the syntax and the high-level data types and operations
  3568. (list slicing etc.).  Do people think this characteristic of the
  3569. tutorial should be changed in favor of a more agressive explanation of
  3570. OO features?  (I have heard people say that the tutorial is a
  3571. "read-once" document -- perhaps a real user's guide is needed, at a
  3572. level between the tutorial and the reference?)
  3573.  
  3574. (Here starts Steve's next mail):
  3575.  
  3576. >Actually, while writing this note, I have been swapping between
  3577. >emacs & python to try examples, and I finally figured out how to 
  3578. >do what I want ( But I will go ahead and send this, instead of 
  3579. >just saying "Nevermind", for discussion sake. ):
  3580.  
  3581. You can do that in private e-mail, but perhaps you should edit your
  3582. messages to the list a little more carefully.  BTW, have you read the
  3583. document misc/CLASSES which comes with the Python distribution?  That
  3584. should have made several points you are slowly discovering here clear
  3585. from the start (if not, please point out where it fails to be clear).
  3586.  
  3587. >I *still* think it would be nice if there was a way to MAKE classes 
  3588. >become initialized when created  as part of their definition, 
  3589. >rather that by explicitly calling an init() function. ( Is there 
  3590. >a way that I'm missing ? ) But I can probably live with the above 
  3591. >method.
  3592.  
  3593. Class initialization in Python does not have the greatest syntax.
  3594. This is one point where I was experimenting with language design.
  3595.  
  3596. Perhaps I could adopt the Modula-3 way (after all I stole enough from
  3597. M3 already): a built-in function new() with as its first argument a
  3598. class name and as further arguments the arguments to class's init
  3599. code.  We also need a way for the user to specify the init code; a
  3600. function with a standard name ("init" comes to mind :-) seems fine to
  3601. me.
  3602.  
  3603. Note that the statements executed in the class definition cannot be
  3604. used for this purpose -- these statements are executed *once*, when
  3605. the class is defined, and their main purpose is to execute the method
  3606. definitions (since a 'def' statement must have been executed in Python
  3607. before it takes effect).
  3608.  
  3609. Finally, on the issue of the disappearing Pythons:
  3610.  
  3611. >    ftp.cwi.nl:/pub/python0.9.2beta.tar.Z
  3612.  
  3613. is indeed tha latest official release and identical to those that once
  3614. were on wuarchive.  I think I cannot blame the archive keepers for
  3615. deleting old files every once in a while.
  3616.  
  3617. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  3618. "I could be arguing in my spare time."
  3619. 
  3620. 
  3621. Received: by charon.cwi.nl with SMTP; Tue, 24 Dec 1991 12:02:44 +0100
  3622. Received: by voorn.cwi.nl with SMTP; Tue, 24 Dec 1991 11:02:43 GMT
  3623. Message-Id: <9112241102.AA05630@voorn.cwi.nl>
  3624. To: python-list@cwi.nl
  3625. Subject: Re: Steve Majewski's mail
  3626. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  3627. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  3628. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  3629. Date: Tue, 24 Dec 91 12:02:43 +0100
  3630. Sender: Guido.van.Rossum@cwi.nl
  3631.  
  3632. Steve Majewski replied on the list to a message not sent to the list;
  3633. I hope y'all list readers can figure out the issues from what he quoted.
  3634. I'll only reply to points that are still unclear.
  3635.  
  3636. > Is 0.9.4 ready and available yet ?
  3637.  
  3638. Good question.  I could either release what I currently have as
  3639. "0.9.4" or stick to it a little longer.  My main reason for not
  3640. releasing it is that I still (sigh) haven't found the time to update
  3641. all the documentation.  However a new release since 0.9.2beta is long
  3642. overdue so maybe I'll release it *without* up-to-date documentation.
  3643. I will post an announcement with a summary of changes on the list when
  3644. it's ready.  People who have a strong opinion about releasing or not
  3645. releasing should send mail to me privately so I can just tally the
  3646. votes.
  3647.  
  3648. > Re: '==' replacing '=' for comparison
  3649. >
  3650. > Yes. That was on my list of minor complaints. I don't think comparison
  3651. > and assignment should have the same symbol. If changing also makes 
  3652. > the grammar/parser easier, then go ahead!  '==' for equality is 
  3653. > probably better that ':=' for assignment, as assignment is probably
  3654. > the more frequent operation.
  3655.  
  3656. I agree, reluctantly (because of all the lines of library and example
  3657. code I will have to change), that this change should be made; but it
  3658. won't be in 0.9.4 if I release that soon.  Note that this still won't
  3659. give you assignments in expressions as side effects; I am against such
  3660. a feature even though it occasionally saves a line of code -- one of
  3661. Python's strong points is that it is usually very easy (and pretty!)
  3662. to read, and I feel that assignments in expressions would have a
  3663. strong negative effect on readability (and prettiness!).
  3664.  
  3665. > What is needed is to merge some of the points discussed here into the
  3666. > users guide - a prominent part of the intro should explain what is 
  3667. > different about python - with a more in depth treatment in the Lang. 
  3668. > Reference Manual.
  3669.  
  3670. The current "tutorial" tries to keep object-oriented features out of
  3671. sight for as long as possible, in order to explain other pertinent
  3672. features like the syntax and the high-level data types and operations
  3673. (list slicing etc.).  Do people think this characteristic of the
  3674. tutorial should be changed in favor of a more agressive explanation of
  3675. OO features?  (I have heard people say that the tutorial is a
  3676. "read-once" document -- perhaps a real user's guide is needed, at a
  3677. level between the tutorial and the reference?)
  3678.  
  3679. (Here starts Steve's next mail):
  3680.  
  3681. >Actually, while writing this note, I have been swapping between
  3682. >emacs & python to try examples, and I finally figured out how to 
  3683. >do what I want ( But I will go ahead and send this, instead of 
  3684. >just saying "Nevermind", for discussion sake. ):
  3685.  
  3686. You can do that in private e-mail, but perhaps you should edit your
  3687. messages to the list a little more carefully.  BTW, have you read the
  3688. document misc/CLASSES which comes with the Python distribution?  That
  3689. should have made several points you are slowly discovering here clear
  3690. from the start (if not, please point out where it fails to be clear).
  3691.  
  3692. >I *still* think it would be nice if there was a way to MAKE classes 
  3693. >become initialized when created  as part of their definition, 
  3694. >rather that by explicitly calling an init() function. ( Is there 
  3695. >a way that I'm missing ? ) But I can probably live with the above 
  3696. >method.
  3697.  
  3698. Class initialization in Python does not have the greatest syntax.
  3699. This is one point where I was experimenting with language design.
  3700.  
  3701. Perhaps I could adopt the Modula-3 way (after all I stole enough from
  3702. M3 already): a built-in function new() with as its first argument a
  3703. class name and as further arguments the arguments to class's init
  3704. code.  We also need a way for the user to specify the init code; a
  3705. function with a standard name ("init" comes to mind :-) seems fine to
  3706. me.
  3707.  
  3708. Note that the statements executed in the class definition cannot be
  3709. used for this purpose -- these statements are executed *once*, when
  3710. the class is defined, and their main purpose is to execute the method
  3711. definitions (since a 'def' statement must have been executed in Python
  3712. before it takes effect).
  3713.  
  3714. Finally, on the issue of the disappearing Pythons:
  3715.  
  3716. >    ftp.cwi.nl:/pub/python0.9.2beta.tar.Z
  3717.  
  3718. is indeed tha latest official release and identical to those that once
  3719. were on wuarchive.  I think I cannot blame the archive keepers for
  3720. deleting old files every once in a while.
  3721.  
  3722. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  3723. "I could be arguing in my spare time."
  3724. 
  3725. 
  3726. Received: by charon.cwi.nl with SMTP; Tue, 24 Dec 1991 13:20:19 +0100
  3727. Received: by voorn.cwi.nl with SMTP; Tue, 24 Dec 1991 12:20:18 GMT
  3728. Message-Id: <9112241220.AA05959@voorn.cwi.nl>
  3729. To: python-list@cwi.nl
  3730. Subject: Python 0.9.4alpha under the Christmas tree!
  3731. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  3732. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  3733. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  3734. Date: Tue, 24 Dec 91 13:20:17 +0100
  3735. Sender: Guido.van.Rossum@cwi.nl
  3736.  
  3737. After all I think it's better to release 0.9.4 now to see what people
  3738. think of it anyway, rather than waiting forever until I find the time
  3739. to perfect the documentation (it certainly isn't going to happen over
  3740. Christmas).
  3741.  
  3742. I've uploaded it to the following three places:
  3743.  
  3744.     host            directory
  3745.  
  3746.     uunet.uu.net        /tmp
  3747.     wuarchive.wustl.edu    /pub
  3748.     ftp.cwi.nl        /pub
  3749.  
  3750. You should fetch the file python0.9.4alpha.tar.Z.  If you haven't got
  3751. STDWIN yet, you can fetch that too, from the same locations; the file
  3752. is stdwin0.9.6.tar.Z.  I don't know what's an appropriate place to get
  3753. bash/readline from, sorry.
  3754.  
  3755. I don't know how long the uunet and wuarchive versions will stay
  3756. around; I have some control over ftp.cwi.nl so I think those versions
  3757. will stay put, but people in North America should first try either US
  3758. site to avoid excessive transatlantic traffic (and Europeans should
  3759. try ftp.cwi.nl first!).
  3760.  
  3761. Don't ask me to mail it to you; find a friend with Internet ftp access
  3762. instead.
  3763.  
  3764. Below are the PREFACE and NEWS files from the distribution (directory
  3765. misc):
  3766.  
  3767. ---------------------------------- NEWS --------------------------------
  3768. This is the announcement of Python 0.9.4 alpha.  I call it an alpha
  3769. release because it is not widely distributed, only to readers of the
  3770. Python mailing list, and because the documentation does not yet
  3771. describe the new features yet.  However, installation should be
  3772. smooth, and I don't expect there are obvious bugs left.
  3773.  
  3774. In particular, these changes are not yet documented:
  3775.  
  3776. - New class syntax, without extraneous parentheses.
  3777. - New 'global' statement to assign global variables from within a function.
  3778. - New, more refined exceptions.
  3779. - New argument passing semantics.
  3780.  
  3781. The release can be picked up by anonymous ftp from site ftp.cwi.nl,
  3782. directory pub, file python0.9.4beta.tar.Z.  I will also upload
  3783. versions to wuarchive.wustl.edu:/pub and to uunet.uu.net:/tmp.  Please
  3784. don't ask me to mail it to you; find a friend with Internet ftp access
  3785. instead.
  3786.  
  3787. The rest of this file contains explanation for the changes listed above.
  3788.  
  3789. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  3790. "Oh Bevis!  And I thought you were so rugged!"
  3791.  
  3792.  
  3793. New class syntax
  3794. ----------------
  3795.  
  3796. You can now declare a base class as follows:
  3797.  
  3798.     class B:            # Was: class B():
  3799.         def some_method(self): ...
  3800.         ...
  3801.  
  3802. and a derived class thusly:
  3803.  
  3804.     class D(B):            # Was: class D() = B():
  3805.         def another_method(self, arg): ...
  3806.  
  3807. Multiple inheritance looks like this:
  3808.  
  3809.     class M(B, D):            # Was: class M() = B(), D():
  3810.         def this_or_that_method(self, arg): ...
  3811.  
  3812. The old syntax is still accepted by Python 0.9.4, but will disappear
  3813. in Python 1.0 (to be posted to comp.sources).
  3814.  
  3815.  
  3816. New 'global' statement
  3817. ----------------------
  3818.  
  3819. Every now and then you have a global variable in a module that you
  3820. want to change from within a function in that module -- say, a count
  3821. of calls to a function, or an option flag, etc.  Until now this was
  3822. not directly possible.  While several kludges are known that
  3823. circumvent the problem, and often the need for a global variable can
  3824. be avoided by rewriting the module as a class, this does not always
  3825. lead to clearer code.
  3826.  
  3827. The 'global' statement solves this dilemma.  Its occurrence in a
  3828. function body means that, for the duration of that function, the
  3829. names listed there refer to global variables.  For instance:
  3830.  
  3831.     total = 0.0
  3832.     count = 0
  3833.  
  3834.     def add_to_total(amount):
  3835.         global total, count
  3836.         total = total + amount
  3837.         count = count + 1
  3838.  
  3839. 'global' must be repeated in each function where it is needed.  The
  3840. names listed in a 'global' statement must not be used in the function
  3841. before the statement is reached.
  3842.  
  3843. Remember that you don't need to use 'global' if you only want to *use*
  3844. a global variable in a function; nor do you need ot for assignments to
  3845. parts of global variables (e.g., list or dictionary items or
  3846. attributes of class instances).  This has not changed; in fact
  3847. assignment to part of a global variable was the standard workaround.
  3848.  
  3849.  
  3850. New exceptions
  3851. --------------
  3852.  
  3853. Several new exceptions have been defined, to distinguish more clearly
  3854. between different types of errors.
  3855.  
  3856. name            meaning                    was
  3857.  
  3858. AttributeError        reference to non-existing attribute    NameError
  3859. IOError            unexpected I/O error            RuntimeError
  3860. ImportError        import of non-existing module or name    NameError
  3861. IndexError        invalid string, tuple or list index    RuntimeError
  3862. KeyError        key not in dictionary            RuntimeError
  3863. OverflowError        numeric overflow            RuntimeError
  3864. SyntaxError        invalid syntax                RuntimeError
  3865. ValueError        invalid argument value            RuntimeError
  3866. ZeroDivisionError    division by zero            RuntimeError
  3867.  
  3868. The string value of each exception is now its name -- this makes it
  3869. easier to experimentally find out which operations raise which
  3870. exceptions; e.g.:
  3871.  
  3872.     >>> KeyboardInterrupt
  3873.     'KeyboardInterrupt'
  3874.     >>>
  3875.  
  3876.  
  3877. New argument passing semantics
  3878. ------------------------------
  3879.  
  3880. Off-line discussions with Steve Majewski and Daniel LaLiberte have
  3881. convinced me that Python's parameter mechanism could be changed in a
  3882. way that made both of them happy (I hope), kept me happy, fixed a
  3883. number of outstanding problems, and, given some backward compatibility
  3884. provisions, would only break a very small amount of existing code --
  3885. probably all mine anyway.  In fact I suspect that most Python users
  3886. will hardly notice the difference.  And yet it has cost me at least
  3887. one sleepless night to decide to make the change...
  3888.  
  3889. Philosophically, the change is quite radical (to me, anyway): a
  3890. function is no longer called with either zero or one argument, which
  3891. is a tuple if there appear to be more arguments.  Every function now
  3892. has an argument list containing 0, 1 or more arguments.  This list is
  3893. always implemented as a tuple, and it is a (run-time) error if a
  3894. function is called with a different number of arguments than expected.
  3895.  
  3896. What's the difference? you may ask.  The answer is, very little unless
  3897. you want to write variadic functions -- functions that may be called
  3898. with a variable number of arguments.  Formerly, you could write a
  3899. function that accepted one or more arguments with little trouble, but
  3900. writing a function that could be called with either 0 or 1 argument
  3901. (or more) was next to impossible.  This is now a piece of cake: you
  3902. can simply declare an argument that receives the entire argument
  3903. tuple, and check its length -- it will be of size 0 if there are no
  3904. arguments.
  3905.  
  3906. Another anomaly of the old system was the way multi-argument methods
  3907. (in classes) had to be declared, e.g.:
  3908.  
  3909.     class Point():
  3910.         def init(self, (x, y, color)): ...
  3911.         def setcolor(self, color): ...
  3912.         dev moveto(self, (x, y)): ...
  3913.         def draw(self): ...
  3914.  
  3915. Using the new scheme there is no need to enclose the method arguments
  3916. in an extra set of parentheses, so the above class could become:
  3917.  
  3918.     class Point:
  3919.         def init(self, x, y, color): ...
  3920.         def setcolor(self, color): ...
  3921.         dev moveto(self, x, y): ...
  3922.         def draw(self): ...
  3923.  
  3924. That is, the equivalence rule between methods and functions has
  3925. changed so that now p.moveto(x,y) is equivalent to Point.moveto(p,x,y)
  3926. while formerly it was equivalent to Point.moveto(p,(x,y)).
  3927.  
  3928. A special backward compatibility rule makes that the old version also
  3929. still works: whenever a function with exactly two arguments (at the top
  3930. level) is called with more than two arguments, the second and further
  3931. arguments are packed into a tuple and passed as the second argument.
  3932. This rule is invoked independently of whether the function is actually a
  3933. method, so there is a slight chance that some erroneous calls of
  3934. functions expecting two arguments with more than that number of
  3935. arguments go undetected at first -- when the function tries to use the
  3936. second argument it may find it is a tuple instead of what was expected.
  3937. Note that this rule will be removed from future versions of the
  3938. language; it is a backward compatibility provision *only*.
  3939.  
  3940. Two other rules and a new built-in function handle conversion between
  3941. tuples and argument lists:
  3942.  
  3943. Rule (a): when a function with more than one argument is called with a
  3944. single argument that is a tuple of the right size, the tuple's items
  3945. are used as arguments.
  3946.  
  3947. Rule (b): when a function with exactly one argument receives no
  3948. arguments or more than one, that one argument will receive a tuple
  3949. containing the arguments (the tuple will be empty if there were no
  3950. arguments).
  3951.  
  3952.  
  3953. While no new argument syntax was added in this phase, it would now be
  3954. quite sensible to add explicit syntax to Python for default argument
  3955. values (as in C++ or Modula-3), or a "rest" argument to receive the
  3956. remaining arguments of a variable-length argument list.
  3957.  
  3958. -------------------------------- PREFACE -------------------------------
  3959. This is Python release 0.9.4 alpha
  3960. ==================================
  3961.  
  3962.  
  3963. What is Python?
  3964. ---------------
  3965.  
  3966. Python is an interpreted prototyping language, incorporating modules,
  3967. exceptions, dynamic typing, very high level dynamic data types, and
  3968. classes.  Python combines remarkable power with very clear syntax.  It
  3969. has interfaces to many system calls and libraries, as well as to
  3970. various window systems, and is extensible in C.  It is also usable as
  3971. an extension language for applications that need a programmable
  3972. interface.  Finally, Python is portable: it runs on many brands of
  3973. UNIX, on the Mac, and maybe on MS-DOS.
  3974.  
  3975.  
  3976. About this release
  3977. ------------------
  3978.  
  3979. Release 0.9.4:
  3980.  
  3981. - new function argument handling
  3982. - new exception string values (NameError = 'NameError' etc.)
  3983. - better checking for math exceptions
  3984. - for sequences (string/tuple/list), x[-i] is now equivalent to x[len(x)-i]
  3985. - fixed list assignment bug: "a[1:1] = a" now works correctly
  3986.  
  3987. Release 0.9.3:
  3988.  
  3989. Release 0.9.3 provides significant new functionality, improves
  3990. efficiency, fixes many bugs, and provides increased portability of the
  3991. Python interpreter.  The syntax proper has changed only slightly since
  3992. 0.9.2beta: a new reserved word, 'global', has been added, and there is
  3993. new syntax for specifying the base classes in a class declarations.
  3994.  
  3995. Highlights of new things in 0.9.3 since 0.9.2:
  3996.  
  3997. - string sys.version shows current version (also printed on interactive entry)
  3998. - more detailed exceptions, e.g., IOError, ZeroDivisionError, etc.
  3999. - 'global' statement to declare module-global variables assigned in functions.
  4000. - new class declaration syntax: class C(Base1, Base2, ...): suite
  4001.   (the old syntax is still accepted -- be sure to convert your classes now!)
  4002. - C shifting and masking operators: << >> ~ & ^ | (for ints and longs).
  4003. - C comparison operators: == != (the old = and <> remain valid).
  4004. - floating point numbers may now start with a period (e.g., .14).
  4005. - definition of integer division tightened (always truncates towards zero).
  4006. - new builtins hex(x), oct(x) return hex/octal string from (long) integer.
  4007. - new list method l.count(x) returns the number of occurrences of x in l.
  4008. - new SGI module: al (Indigo and 4D/35 audio library).
  4009. - the FORMS interface (modules fl and FL) now uses FORMS 2.0
  4010. - module gl: added lrect{read,write}, rectzoom and pixmode;
  4011.   added (non-GL) functions (un)packrect.
  4012. - new socket method: s.allowbroadcast(flag).
  4013. - many objects support __dict__, __methods__ or __members__.
  4014. - dir() lists anything that has __dict__.
  4015. - class attributes are no longer read-only.
  4016. - classes support __bases__, instances support __class__ (and __dict__).
  4017. - divmod() now also works for floats.
  4018. - fixed obscure bug in eval('1            ').
  4019.  
  4020. These features were new in version 0.9.2beta:
  4021.  
  4022. - tutorial now (almost) complete; library reference reorganized
  4023. - new syntax: continue statement; semicolons; dictionary constructors;
  4024.   restrictions on blank lines in source files removed
  4025. - dramatically improved module load time through precompiled modules
  4026. - arbitrary precision integers: compute 2 to the power 1000 and more...
  4027. - arithmetic operators now accept mixed type operands, e.g., 3.14/4
  4028. - more operations on list: remove, index, reverse; repetition
  4029. - improved/new file operations: readlines, seek, tell, flush, ...
  4030. - process management added to the posix module: fork/exec/wait/kill etc.
  4031. - BSD socket operations (with example servers and clients!)
  4032. - many new STDWIN features (color, fonts, polygons, ...)
  4033. - new SGI modules: font manager and FORMS library interface
  4034.  
  4035.  
  4036. Release history
  4037. ---------------
  4038.  
  4039. Version 0.9.0 was posted in February 1991 to alt.sources, soon
  4040. followed by patches to bring it up to level 0.9.1.
  4041.  
  4042. Compressed tar images of version 0.9.1 was made available for
  4043. anonymous ftp on several widely accessible US Internet sites, and in
  4044. Europe on hp4nl.nluug.nl and mcsun.eu.net.  Since not everybody has
  4045. LaTeX, Postscript of the manuals documentation was also made available
  4046. in this way.  Similar for a binhex'ed Macintosh application.
  4047.  
  4048. Release 0.9.2 never completely materialized, however a version called
  4049. 0.9.2beta had limited distribution.  (There was nothing wrong with it
  4050. except that I was too busy to release it after it went through beta
  4051. testing).
  4052.  
  4053.  
  4054.  
  4055. Ftp availability
  4056. ----------------
  4057.  
  4058. I hope to make the compressed tar image of Python available for
  4059. anonymous ftp on at least the following sites (the filename being
  4060. python0.9.3.tar.Z):
  4061.  
  4062.     site            directory
  4063.  
  4064.     ftp.cwi.nl        pub        (this is the master source)
  4065.     wuarchive.wustl.edu    pub
  4066.     uunet.uu.net        tmp
  4067.  
  4068.  
  4069. Notes for STDWIN users
  4070. ----------------------
  4071.  
  4072. If you are using STDWIN with Python, you should fetch STDWIN 0.9.6,
  4073. available from the same sources as Python -- the new Python provides
  4074. interfaces to all the new STDWIN functionality so it won't link if you
  4075. use the old STDWIN library.
  4076.  
  4077. If you have applied the "unofficial patch" to stdwinmodule.c for the
  4078. Sun4 (inserting return keywords before calls to drawing_generic()),
  4079. you should unapply that patch, else the new patch may get in trouble.
  4080.  
  4081.  
  4082. Overview of this release
  4083. ------------------------
  4084.  
  4085. Here is a summary of the most important user-visible changes in 0.9.2,
  4086. in somewhat arbitrary order.  Changes in 0.9.3 are listed in the
  4087. "highlights" section above.
  4088.  
  4089.  
  4090. 1. Changes to the interpreter proper
  4091.  
  4092. - Simple statements can now be separated by semicolons.
  4093.   If you write "if t: s1; s2", both s1 and s2 are executed
  4094.   conditionally.
  4095. - The 'continue' statement was added, with semantics as in C.
  4096. - Dictionary displays are now allowed on input: {key: value, ...}.
  4097. - Blank lines and lines bearing only a comment no longer need to
  4098.   be indented properly.  (A completely empty line still ends a multi-
  4099.   line statement interactively.)
  4100. - Mixed arithmetic is supported, 1 compares equal to 1.0, etc.
  4101. - Option "-c command" to execute statements from the command line
  4102. - Compiled versions of modules are cached in ".pyc" files, giving a
  4103.   dramatic improvement of start-up time
  4104. - Other, smaller speed improvements, e.g., extracting characters from
  4105.   strings, looking up single-character keys, and looking up global
  4106.   variables
  4107. - Interrupting a print operation raises KeyboardInterrupt instead of
  4108.   only cancelling the print operation
  4109. - Fixed various portability problems (it now passes gcc with only
  4110.   warnings -- more Standard C compatibility will be provided in later
  4111.   versions)
  4112. - Source is prepared for porting to MS-DOS
  4113. - Numeric constants are now checked for overflow (this requires
  4114.   standard-conforming strtol() and strtod() functions; a correct
  4115.   strtol() implementation is provided, but the strtod() provided
  4116.   relies on atof() for everything, including error checking
  4117.  
  4118.  
  4119. 2. Changes to the built-in types, functions and modules
  4120.  
  4121. - New module socket: interface to BSD socket primitives
  4122. - New modules pwd and grp: access the UNIX password and group databases
  4123. - (SGI only:) New module "fm" interfaces to the SGI IRIX Font Manager
  4124. - (SGI only:) New module "fl" interfaces to Mark Overmars' FORMS library
  4125. - New numeric type: long integer, for unlimited precision
  4126.     - integer constants suffixed with 'L' or 'l' are long integers
  4127.     - new built-in function long(x) converts int or float to long
  4128.     - int() and float() now also convert from long integers
  4129. - New built-in function:
  4130.     - pow(x, y) returns x to the power y
  4131. - New operation and methods for lists:
  4132.     - l*n returns a new list consisting of n concatenated copies of l
  4133.     - l.remove(x) removes the first occurrence of the value x from l
  4134.     - l.index(x) returns the index of the first occurrence of x in l
  4135.     - l.reverse() reverses l in place
  4136. - New operation for tuples:
  4137.     - t*n returns a tuple consisting of n concatenated copies of t
  4138. - Improved file handling:
  4139.     - f.readline() no longer restricts the line length, is faster,
  4140.       and isn't confused by null bytes; same for raw_input()
  4141.     - f.read() without arguments reads the entire (rest of the) file
  4142.     - mixing of print and sys.stdout.write() has different effect
  4143. - New methods for files:
  4144.     - f.readlines() returns a list containing the lines of the file,
  4145.       as read with f.readline()
  4146.     - f.flush(), f.tell(), f.seek() call their stdio counterparts
  4147.     - f.isatty() tests for "tty-ness"
  4148. - New posix functions:
  4149.     - _exit(), exec(), fork(), getpid(), getppid(), kill(), wait()
  4150.     - popen() returns a file object connected to a pipe
  4151.     - utime() replaces utimes() (the latter is not a POSIX name)
  4152. - New stdwin features, including:
  4153.     - font handling
  4154.     - color drawing
  4155.     - scroll bars made optional
  4156.     - polygons
  4157.     - filled and xor shapes
  4158.     - text editing objects now have a 'settext' method
  4159.  
  4160.  
  4161. 3. Changes to the standard library
  4162.  
  4163. - Name change: the functions path.cat and macpath.cat are now called
  4164.   path.join and macpath.join
  4165. - Added new modules: formatter, mutex, persist, sched, mainloop
  4166. - Added some modules and functionality to the "widget set" (which is
  4167.   still under development, so please bear with me):
  4168.     DirList, FormSplit, TextEdit, WindowSched
  4169. - Fixed module testall to work non-interactively
  4170. - Module string:
  4171.     - added functions join() and joinfields()
  4172.     - fixed center() to work correct and make it "transitive"
  4173. - Obsolete modules were removed: util, minmax
  4174. - Some modules were moved to the demo directory
  4175.  
  4176.  
  4177. 4. Changes to the demonstration programs
  4178.  
  4179. - Added new useful scipts: byteyears, eptags, fact, from, lfact,
  4180.   objgraph, pdeps, pi, primes, ptags, which
  4181. - Added a bunch of socket demos
  4182. - Doubled the speed of ptags
  4183. - Added new stdwin demos: microedit, miniedit
  4184. - Added a windowing interface to the Python interpreter: python (most
  4185.   useful on the Mac)
  4186. - Added a browser for Emacs info files: demo/stdwin/ibrowse
  4187.   (yes, I plan to put all STDWIN and Python documentation in texinfo
  4188.   form in the future)
  4189.  
  4190.  
  4191. 5. Other changes to the distribution
  4192.  
  4193. - An Emacs Lisp file "pythonmode.el" is provided to facilitate editing
  4194.   Python programs in GNU Emacs (slightly improved since posted to
  4195.   gnu.emacs.sources)
  4196. - Some info on writing an extension in C is provided
  4197. - Some info on building Python on non-UNIX platforms is provided
  4198. ------------------------------------------------------------------------
  4199.  
  4200. Happy hacking Christmas,
  4201.  
  4202. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  4203. "Wenn ist das Nunnstueck git und Slotermeyer?"
  4204. "Ja! Beierhund das oder die flipperwaldt gersput."
  4205. 
  4206. 
  4207. To: python-list@cwi.nl
  4208. Subject: Python 0.9.4alpha under the Christmas tree!
  4209. From: Guido van Rossum <guido@cwi.nl>
  4210. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  4211. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  4212. Date: Tue, 24 Dec 91 13:20:17 +0100
  4213. Sender: guido
  4214.  
  4215. After all I think it's better to release 0.9.4 now to see what people
  4216. think of it anyway, rather than waiting forever until I find the time
  4217. to perfect the documentation (it certainly isn't going to happen over
  4218. Christmas).
  4219.  
  4220. I've uploaded it to the following three places:
  4221.  
  4222.     host            directory
  4223.  
  4224.     uunet.uu.net        /tmp
  4225.     wuarchive.wustl.edu    /pub
  4226.     ftp.cwi.nl        /pub
  4227.  
  4228. You should fetch the file python0.9.4alpha.tar.Z.  If you haven't got
  4229. STDWIN yet, you can fetch that too, from the same locations; the file
  4230. is stdwin0.9.6.tar.Z.  I don't know what's an appropriate place to get
  4231. bash/readline from, sorry.
  4232.  
  4233. I don't know how long the uunet and wuarchive versions will stay
  4234. around; I have some control over ftp.cwi.nl so I think those versions
  4235. will stay put, but people in North America should first try either US
  4236. site to avoid excessive transatlantic traffic (and Europeans should
  4237. try ftp.cwi.nl first!).
  4238.  
  4239. Don't ask me to mail it to you; find a friend with Internet ftp access
  4240. instead.
  4241.  
  4242. Below are the PREFACE and NEWS files from the distribution (directory
  4243. misc):
  4244.  
  4245. ---------------------------------- NEWS --------------------------------
  4246. This is the announcement of Python 0.9.4 alpha.  I call it an alpha
  4247. release because it is not widely distributed, only to readers of the
  4248. Python mailing list, and because the documentation does not yet
  4249. describe the new features yet.  However, installation should be
  4250. smooth, and I don't expect there are obvious bugs left.
  4251.  
  4252. In particular, these changes are not yet documented:
  4253.  
  4254. - New class syntax, without extraneous parentheses.
  4255. - New 'global' statement to assign global variables from within a function.
  4256. - New, more refined exceptions.
  4257. - New argument passing semantics.
  4258.  
  4259. The release can be picked up by anonymous ftp from site ftp.cwi.nl,
  4260. directory pub, file python0.9.4beta.tar.Z.  I will also upload
  4261. versions to wuarchive.wustl.edu:/pub and to uunet.uu.net:/tmp.  Please
  4262. don't ask me to mail it to you; find a friend with Internet ftp access
  4263. instead.
  4264.  
  4265. The rest of this file contains explanation for the changes listed above.
  4266.  
  4267. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  4268. "Oh Bevis!  And I thought you were so rugged!"
  4269.  
  4270.  
  4271. New class syntax
  4272. ----------------
  4273.  
  4274. You can now declare a base class as follows:
  4275.  
  4276.     class B:            # Was: class B():
  4277.         def some_method(self): ...
  4278.         ...
  4279.  
  4280. and a derived class thusly:
  4281.  
  4282.     class D(B):            # Was: class D() = B():
  4283.         def another_method(self, arg): ...
  4284.  
  4285. Multiple inheritance looks like this:
  4286.  
  4287.     class M(B, D):            # Was: class M() = B(), D():
  4288.         def this_or_that_method(self, arg): ...
  4289.  
  4290. The old syntax is still accepted by Python 0.9.4, but will disappear
  4291. in Python 1.0 (to be posted to comp.sources).
  4292.  
  4293.  
  4294. New 'global' statement
  4295. ----------------------
  4296.  
  4297. Every now and then you have a global variable in a module that you
  4298. want to change from within a function in that module -- say, a count
  4299. of calls to a function, or an option flag, etc.  Until now this was
  4300. not directly possible.  While several kludges are known that
  4301. circumvent the problem, and often the need for a global variable can
  4302. be avoided by rewriting the module as a class, this does not always
  4303. lead to clearer code.
  4304.  
  4305. The 'global' statement solves this dilemma.  Its occurrence in a
  4306. function body means that, for the duration of that function, the
  4307. names listed there refer to global variables.  For instance:
  4308.  
  4309.     total = 0.0
  4310.     count = 0
  4311.  
  4312.     def add_to_total(amount):
  4313.         global total, count
  4314.         total = total + amount
  4315.         count = count + 1
  4316.  
  4317. 'global' must be repeated in each function where it is needed.  The
  4318. names listed in a 'global' statement must not be used in the function
  4319. before the statement is reached.
  4320.  
  4321. Remember that you don't need to use 'global' if you only want to *use*
  4322. a global variable in a function; nor do you need ot for assignments to
  4323. parts of global variables (e.g., list or dictionary items or
  4324. attributes of class instances).  This has not changed; in fact
  4325. assignment to part of a global variable was the standard workaround.
  4326.  
  4327.  
  4328. New exceptions
  4329. --------------
  4330.  
  4331. Several new exceptions have been defined, to distinguish more clearly
  4332. between different types of errors.
  4333.  
  4334. name            meaning                    was
  4335.  
  4336. AttributeError        reference to non-existing attribute    NameError
  4337. IOError            unexpected I/O error            RuntimeError
  4338. ImportError        import of non-existing module or name    NameError
  4339. IndexError        invalid string, tuple or list index    RuntimeError
  4340. KeyError        key not in dictionary            RuntimeError
  4341. OverflowError        numeric overflow            RuntimeError
  4342. SyntaxError        invalid syntax                RuntimeError
  4343. ValueError        invalid argument value            RuntimeError
  4344. ZeroDivisionError    division by zero            RuntimeError
  4345.  
  4346. The string value of each exception is now its name -- this makes it
  4347. easier to experimentally find out which operations raise which
  4348. exceptions; e.g.:
  4349.  
  4350.     >>> KeyboardInterrupt
  4351.     'KeyboardInterrupt'
  4352.     >>>
  4353.  
  4354.  
  4355. New argument passing semantics
  4356. ------------------------------
  4357.  
  4358. Off-line discussions with Steve Majewski and Daniel LaLiberte have
  4359. convinced me that Python's parameter mechanism could be changed in a
  4360. way that made both of them happy (I hope), kept me happy, fixed a
  4361. number of outstanding problems, and, given some backward compatibility
  4362. provisions, would only break a very small amount of existing code --
  4363. probably all mine anyway.  In fact I suspect that most Python users
  4364. will hardly notice the difference.  And yet it has cost me at least
  4365. one sleepless night to decide to make the change...
  4366.  
  4367. Philosophically, the change is quite radical (to me, anyway): a
  4368. function is no longer called with either zero or one argument, which
  4369. is a tuple if there appear to be more arguments.  Every function now
  4370. has an argument list containing 0, 1 or more arguments.  This list is
  4371. always implemented as a tuple, and it is a (run-time) error if a
  4372. function is called with a different number of arguments than expected.
  4373.  
  4374. What's the difference? you may ask.  The answer is, very little unless
  4375. you want to write variadic functions -- functions that may be called
  4376. with a variable number of arguments.  Formerly, you could write a
  4377. function that accepted one or more arguments with little trouble, but
  4378. writing a function that could be called with either 0 or 1 argument
  4379. (or more) was next to impossible.  This is now a piece of cake: you
  4380. can simply declare an argument that receives the entire argument
  4381. tuple, and check its length -- it will be of size 0 if there are no
  4382. arguments.
  4383.  
  4384. Another anomaly of the old system was the way multi-argument methods
  4385. (in classes) had to be declared, e.g.:
  4386.  
  4387.     class Point():
  4388.         def init(self, (x, y, color)): ...
  4389.         def setcolor(self, color): ...
  4390.         dev moveto(self, (x, y)): ...
  4391.         def draw(self): ...
  4392.  
  4393. Using the new scheme there is no need to enclose the method arguments
  4394. in an extra set of parentheses, so the above class could become:
  4395.  
  4396.     class Point:
  4397.         def init(self, x, y, color): ...
  4398.         def setcolor(self, color): ...
  4399.         dev moveto(self, x, y): ...
  4400.         def draw(self): ...
  4401.  
  4402. That is, the equivalence rule between methods and functions has
  4403. changed so that now p.moveto(x,y) is equivalent to Point.moveto(p,x,y)
  4404. while formerly it was equivalent to Point.moveto(p,(x,y)).
  4405.  
  4406. A special backward compatibility rule makes that the old version also
  4407. still works: whenever a function with exactly two arguments (at the top
  4408. level) is called with more than two arguments, the second and further
  4409. arguments are packed into a tuple and passed as the second argument.
  4410. This rule is invoked independently of whether the function is actually a
  4411. method, so there is a slight chance that some erroneous calls of
  4412. functions expecting two arguments with more than that number of
  4413. arguments go undetected at first -- when the function tries to use the
  4414. second argument it may find it is a tuple instead of what was expected.
  4415. Note that this rule will be removed from future versions of the
  4416. language; it is a backward compatibility provision *only*.
  4417.  
  4418. Two other rules and a new built-in function handle conversion between
  4419. tuples and argument lists:
  4420.  
  4421. Rule (a): when a function with more than one argument is called with a
  4422. single argument that is a tuple of the right size, the tuple's items
  4423. are used as arguments.
  4424.  
  4425. Rule (b): when a function with exactly one argument receives no
  4426. arguments or more than one, that one argument will receive a tuple
  4427. containing the arguments (the tuple will be empty if there were no
  4428. arguments).
  4429.  
  4430.  
  4431. While no new argument syntax was added in this phase, it would now be
  4432. quite sensible to add explicit syntax to Python for default argument
  4433. values (as in C++ or Modula-3), or a "rest" argument to receive the
  4434. remaining arguments of a variable-length argument list.
  4435.  
  4436. -------------------------------- PREFACE -------------------------------
  4437. This is Python release 0.9.4 alpha
  4438. ==================================
  4439.  
  4440.  
  4441. What is Python?
  4442. ---------------
  4443.  
  4444. Python is an interpreted prototyping language, incorporating modules,
  4445. exceptions, dynamic typing, very high level dynamic data types, and
  4446. classes.  Python combines remarkable power with very clear syntax.  It
  4447. has interfaces to many system calls and libraries, as well as to
  4448. various window systems, and is extensible in C.  It is also usable as
  4449. an extension language for applications that need a programmable
  4450. interface.  Finally, Python is portable: it runs on many brands of
  4451. UNIX, on the Mac, and maybe on MS-DOS.
  4452.  
  4453.  
  4454. About this release
  4455. ------------------
  4456.  
  4457. Release 0.9.4:
  4458.  
  4459. - new function argument handling
  4460. - new exception string values (NameError = 'NameError' etc.)
  4461. - better checking for math exceptions
  4462. - for sequences (string/tuple/list), x[-i] is now equivalent to x[len(x)-i]
  4463. - fixed list assignment bug: "a[1:1] = a" now works correctly
  4464.  
  4465. Release 0.9.3:
  4466.  
  4467. Release 0.9.3 provides significant new functionality, improves
  4468. efficiency, fixes many bugs, and provides increased portability of the
  4469. Python interpreter.  The syntax proper has changed only slightly since
  4470. 0.9.2beta: a new reserved word, 'global', has been added, and there is
  4471. new syntax for specifying the base classes in a class declarations.
  4472.  
  4473. Highlights of new things in 0.9.3 since 0.9.2:
  4474.  
  4475. - string sys.version shows current version (also printed on interactive entry)
  4476. - more detailed exceptions, e.g., IOError, ZeroDivisionError, etc.
  4477. - 'global' statement to declare module-global variables assigned in functions.
  4478. - new class declaration syntax: class C(Base1, Base2, ...): suite
  4479.   (the old syntax is still accepted -- be sure to convert your classes now!)
  4480. - C shifting and masking operators: << >> ~ & ^ | (for ints and longs).
  4481. - C comparison operators: == != (the old = and <> remain valid).
  4482. - floating point numbers may now start with a period (e.g., .14).
  4483. - definition of integer division tightened (always truncates towards zero).
  4484. - new builtins hex(x), oct(x) return hex/octal string from (long) integer.
  4485. - new list method l.count(x) returns the number of occurrences of x in l.
  4486. - new SGI module: al (Indigo and 4D/35 audio library).
  4487. - the FORMS interface (modules fl and FL) now uses FORMS 2.0
  4488. - module gl: added lrect{read,write}, rectzoom and pixmode;
  4489.   added (non-GL) functions (un)packrect.
  4490. - new socket method: s.allowbroadcast(flag).
  4491. - many objects support __dict__, __methods__ or __members__.
  4492. - dir() lists anything that has __dict__.
  4493. - class attributes are no longer read-only.
  4494. - classes support __bases__, instances support __class__ (and __dict__).
  4495. - divmod() now also works for floats.
  4496. - fixed obscure bug in eval('1            ').
  4497.  
  4498. These features were new in version 0.9.2beta:
  4499.  
  4500. - tutorial now (almost) complete; library reference reorganized
  4501. - new syntax: continue statement; semicolons; dictionary constructors;
  4502.   restrictions on blank lines in source files removed
  4503. - dramatically improved module load time through precompiled modules
  4504. - arbitrary precision integers: compute 2 to the power 1000 and more...
  4505. - arithmetic operators now accept mixed type operands, e.g., 3.14/4
  4506. - more operations on list: remove, index, reverse; repetition
  4507. - improved/new file operations: readlines, seek, tell, flush, ...
  4508. - process management added to the posix module: fork/exec/wait/kill etc.
  4509. - BSD socket operations (with example servers and clients!)
  4510. - many new STDWIN features (color, fonts, polygons, ...)
  4511. - new SGI modules: font manager and FORMS library interface
  4512.  
  4513.  
  4514. Release history
  4515. ---------------
  4516.  
  4517. Version 0.9.0 was posted in February 1991 to alt.sources, soon
  4518. followed by patches to bring it up to level 0.9.1.
  4519.  
  4520. Compressed tar images of version 0.9.1 was made available for
  4521. anonymous ftp on several widely accessible US Internet sites, and in
  4522. Europe on hp4nl.nluug.nl and mcsun.eu.net.  Since not everybody has
  4523. LaTeX, Postscript of the manuals documentation was also made available
  4524. in this way.  Similar for a binhex'ed Macintosh application.
  4525.  
  4526. Release 0.9.2 never completely materialized, however a version called
  4527. 0.9.2beta had limited distribution.  (There was nothing wrong with it
  4528. except that I was too busy to release it after it went through beta
  4529. testing).
  4530.  
  4531.  
  4532.  
  4533. Ftp availability
  4534. ----------------
  4535.  
  4536. I hope to make the compressed tar image of Python available for
  4537. anonymous ftp on at least the following sites (the filename being
  4538. python0.9.3.tar.Z):
  4539.  
  4540.     site            directory
  4541.  
  4542.     ftp.cwi.nl        pub        (this is the master source)
  4543.     wuarchive.wustl.edu    pub
  4544.     uunet.uu.net        tmp
  4545.  
  4546.  
  4547. Notes for STDWIN users
  4548. ----------------------
  4549.  
  4550. If you are using STDWIN with Python, you should fetch STDWIN 0.9.6,
  4551. available from the same sources as Python -- the new Python provides
  4552. interfaces to all the new STDWIN functionality so it won't link if you
  4553. use the old STDWIN library.
  4554.  
  4555. If you have applied the "unofficial patch" to stdwinmodule.c for the
  4556. Sun4 (inserting return keywords before calls to drawing_generic()),
  4557. you should unapply that patch, else the new patch may get in trouble.
  4558.  
  4559.  
  4560. Overview of this release
  4561. ------------------------
  4562.  
  4563. Here is a summary of the most important user-visible changes in 0.9.2,
  4564. in somewhat arbitrary order.  Changes in 0.9.3 are listed in the
  4565. "highlights" section above.
  4566.  
  4567.  
  4568. 1. Changes to the interpreter proper
  4569.  
  4570. - Simple statements can now be separated by semicolons.
  4571.   If you write "if t: s1; s2", both s1 and s2 are executed
  4572.   conditionally.
  4573. - The 'continue' statement was added, with semantics as in C.
  4574. - Dictionary displays are now allowed on input: {key: value, ...}.
  4575. - Blank lines and lines bearing only a comment no longer need to
  4576.   be indented properly.  (A completely empty line still ends a multi-
  4577.   line statement interactively.)
  4578. - Mixed arithmetic is supported, 1 compares equal to 1.0, etc.
  4579. - Option "-c command" to execute statements from the command line
  4580. - Compiled versions of modules are cached in ".pyc" files, giving a
  4581.   dramatic improvement of start-up time
  4582. - Other, smaller speed improvements, e.g., extracting characters from
  4583.   strings, looking up single-character keys, and looking up global
  4584.   variables
  4585. - Interrupting a print operation raises KeyboardInterrupt instead of
  4586.   only cancelling the print operation
  4587. - Fixed various portability problems (it now passes gcc with only
  4588.   warnings -- more Standard C compatibility will be provided in later
  4589.   versions)
  4590. - Source is prepared for porting to MS-DOS
  4591. - Numeric constants are now checked for overflow (this requires
  4592.   standard-conforming strtol() and strtod() functions; a correct
  4593.   strtol() implementation is provided, but the strtod() provided
  4594.   relies on atof() for everything, including error checking
  4595.  
  4596.  
  4597. 2. Changes to the built-in types, functions and modules
  4598.  
  4599. - New module socket: interface to BSD socket primitives
  4600. - New modules pwd and grp: access the UNIX password and group databases
  4601. - (SGI only:) New module "fm" interfaces to the SGI IRIX Font Manager
  4602. - (SGI only:) New module "fl" interfaces to Mark Overmars' FORMS library
  4603. - New numeric type: long integer, for unlimited precision
  4604.     - integer constants suffixed with 'L' or 'l' are long integers
  4605.     - new built-in function long(x) converts int or float to long
  4606.     - int() and float() now also convert from long integers
  4607. - New built-in function:
  4608.     - pow(x, y) returns x to the power y
  4609. - New operation and methods for lists:
  4610.     - l*n returns a new list consisting of n concatenated copies of l
  4611.     - l.remove(x) removes the first occurrence of the value x from l
  4612.     - l.index(x) returns the index of the first occurrence of x in l
  4613.     - l.reverse() reverses l in place
  4614. - New operation for tuples:
  4615.     - t*n returns a tuple consisting of n concatenated copies of t
  4616. - Improved file handling:
  4617.     - f.readline() no longer restricts the line length, is faster,
  4618.       and isn't confused by null bytes; same for raw_input()
  4619.     - f.read() without arguments reads the entire (rest of the) file
  4620.     - mixing of print and sys.stdout.write() has different effect
  4621. - New methods for files:
  4622.     - f.readlines() returns a list containing the lines of the file,
  4623.       as read with f.readline()
  4624.     - f.flush(), f.tell(), f.seek() call their stdio counterparts
  4625.     - f.isatty() tests for "tty-ness"
  4626. - New posix functions:
  4627.     - _exit(), exec(), fork(), getpid(), getppid(), kill(), wait()
  4628.     - popen() returns a file object connected to a pipe
  4629.     - utime() replaces utimes() (the latter is not a POSIX name)
  4630. - New stdwin features, including:
  4631.     - font handling
  4632.     - color drawing
  4633.     - scroll bars made optional
  4634.     - polygons
  4635.     - filled and xor shapes
  4636.     - text editing objects now have a 'settext' method
  4637.  
  4638.  
  4639. 3. Changes to the standard library
  4640.  
  4641. - Name change: the functions path.cat and macpath.cat are now called
  4642.   path.join and macpath.join
  4643. - Added new modules: formatter, mutex, persist, sched, mainloop
  4644. - Added some modules and functionality to the "widget set" (which is
  4645.   still under development, so please bear with me):
  4646.     DirList, FormSplit, TextEdit, WindowSched
  4647. - Fixed module testall to work non-interactively
  4648. - Module string:
  4649.     - added functions join() and joinfields()
  4650.     - fixed center() to work correct and make it "transitive"
  4651. - Obsolete modules were removed: util, minmax
  4652. - Some modules were moved to the demo directory
  4653.  
  4654.  
  4655. 4. Changes to the demonstration programs
  4656.  
  4657. - Added new useful scipts: byteyears, eptags, fact, from, lfact,
  4658.   objgraph, pdeps, pi, primes, ptags, which
  4659. - Added a bunch of socket demos
  4660. - Doubled the speed of ptags
  4661. - Added new stdwin demos: microedit, miniedit
  4662. - Added a windowing interface to the Python interpreter: python (most
  4663.   useful on the Mac)
  4664. - Added a browser for Emacs info files: demo/stdwin/ibrowse
  4665.   (yes, I plan to put all STDWIN and Python documentation in texinfo
  4666.   form in the future)
  4667.  
  4668.  
  4669. 5. Other changes to the distribution
  4670.  
  4671. - An Emacs Lisp file "pythonmode.el" is provided to facilitate editing
  4672.   Python programs in GNU Emacs (slightly improved since posted to
  4673.   gnu.emacs.sources)
  4674. - Some info on writing an extension in C is provided
  4675. - Some info on building Python on non-UNIX platforms is provided
  4676. ------------------------------------------------------------------------
  4677.  
  4678. Happy hacking Christmas,
  4679.  
  4680. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  4681. "Wenn ist das Nunnstueck git und Slotermeyer?"
  4682. "Ja! Beierhund das oder die flipperwaldt gersput."
  4683. 
  4684. 
  4685. To: hhg1@gte.com (Hallett German)
  4686. Subject: Re: Mac Binary of Python 0.94? 
  4687. In-reply-to: Your message of "Tue, 24 Dec 91 07:39:32 MET."
  4688.              <9112241239.AA15186@bunny.gte.com> 
  4689. From: Guido van Rossum <guido@cwi.nl>
  4690. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  4691. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  4692. Date: Tue, 24 Dec 91 13:42:26 +0100
  4693. Sender: guido
  4694.  
  4695. >Does this bird exist? Thanks for the Xmas present!
  4696.  
  4697. Sorry, not yet -- it's very tedious to upload the Python sources to my
  4698. Mac at home, and I can't come into the office until Jan. 2 1992...
  4699.  
  4700. Cheers,
  4701. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  4702. _   /| 
  4703. \'o.O' 
  4704. =(___)=
  4705.    U   
  4706. 
  4707. 
  4708. Received: by charon.cwi.nl with SMTP; Thu, 26 Dec 1991 12:48:40 +0100
  4709. Received: by voorn.cwi.nl with SMTP; Thu, 26 Dec 1991 11:48:39 GMT
  4710. Message-Id: <9112261148.AA10118@voorn.cwi.nl>
  4711. To: python-list@cwi.nl
  4712. Subject: A script to change to new class syntax
  4713. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  4714. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  4715. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  4716. Date: Thu, 26 Dec 91 12:48:38 +0100
  4717. Sender: Guido.van.Rossum@cwi.nl
  4718.  
  4719. When I made the 0.9.4alpha release I hadn't fixed the library and demo
  4720. programs to use the new class syntax.  Rather than re-issue the
  4721. release, here's the Python script I used to convert Python sources to
  4722. the new syntax.  Call it with either files or directories as
  4723. arguments; it recurses down directories to convert all Python modules
  4724. it can find.  It keeps backups of converted files and is cautious
  4725. about symbolic links.
  4726.  
  4727. The script in itself is an interesting example of recursion down a
  4728. directory tree and using regular expressions (the "regexp" module).
  4729.  
  4730. Install the script as "demo/scripts/classfix.py".  To convert all
  4731. library and demo modules, chdir to the top of the Python tree and
  4732. issue this command:
  4733.  
  4734.     python demo/scripts/classfix.py lib demo
  4735.  
  4736. There is no need to rebuild Python after the script has run.
  4737.  
  4738. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  4739. "No, no really, we're just not religious thank you."
  4740.  
  4741. ------------------------------- cut here -------------------------------
  4742. #! /usr/local/python
  4743.  
  4744. # Fix Python source files to use the new class definition syntax,
  4745. # i.e.,
  4746. #    class C(base, base, ...): ...
  4747. # instead of
  4748. #    class C() = base(), base(), ...: ...
  4749. #
  4750. # Command line arguments are files or directories to be processed.
  4751. # Directories are searched recursively for files whose name looks
  4752. # like a python module.
  4753. # Symbolic links are always ignored (except as explicit directory
  4754. # arguments).  Of course, the original file is kept as a back-up
  4755. # (with a "~" attached to its name).
  4756. #
  4757. # Undoubtedly you can do this using find and sed, but this is
  4758. # a nice example of Python code that recurses down a directory tree
  4759. # and uses regular expressions.  Also note several subtleties like
  4760. # preserving the file's mode and avoiding to even write a temp file
  4761. # when no changes are needed for a file.
  4762. #
  4763. # Changes made are reported to stdout in a diff-like format.
  4764.  
  4765. import sys
  4766. import regexp
  4767. import posix
  4768. import path
  4769. import string
  4770. from stat import *
  4771.  
  4772. err = sys.stderr.write
  4773. dbg = err
  4774. rep = sys.stdout.write
  4775.  
  4776. def main():
  4777.     bad = 0
  4778.     if not sys.argv[1:]: # No arguments
  4779.         err('usage: classfix file-or-directory ...\n')
  4780.         sys.exit(2)
  4781.     for arg in sys.argv[1:]:
  4782.         if path.isdir(arg):
  4783.             if recursedown(arg): bad = 1
  4784.         elif path.islink(arg):
  4785.             err(arg + ': will not process symbolic links\n')
  4786.             bad = 1
  4787.         else:
  4788.             if fix(arg): bad = 1
  4789.     sys.exit(bad)
  4790.  
  4791. ispython = regexp.compile('^[a-zA-Z0-9_]+\.py$').match # This is a method!
  4792.  
  4793. def recursedown(dirname):
  4794.     dbg('recursedown(' + `dirname` + ')\n')
  4795.     bad = 0
  4796.     try:
  4797.         names = posix.listdir(dirname)
  4798.     except posix.error, msg:
  4799.         err(dirname + ': cannot list directory: ' + `msg` + '\n')
  4800.         return 1
  4801.     for name in names:
  4802.         if name in ('.', '..'): continue
  4803.         fullname = path.join(dirname, name)
  4804.         if path.islink(fullname): pass
  4805.         elif path.isdir(fullname):
  4806.             if recursedown(fullname): bad = 1
  4807.         elif ispython(name):
  4808.             if fix(fullname): bad = 1
  4809.     return bad
  4810.  
  4811. # This expression doesn't catch *all* class definition headers,
  4812. # but it's darn pretty close.
  4813. classexpr = '^([ \t]*class +[a-zA-Z0-9_]+) *\( *\) *((=.*)?):'
  4814. findclass = regexp.compile(classexpr).match # This is a method!
  4815.  
  4816. baseexpr = '^ *(.*) *\( *\) *$'
  4817. findbase = regexp.compile(baseexpr).match # This is a method, too!
  4818.  
  4819. def fix(filename):
  4820. ##    dbg('fix(' + `filename` + ')\n')
  4821.     try:
  4822.         f = open(filename, 'r')
  4823.     except IOError, msg:
  4824.         err(filename + ': cannot open: ' + `msg` + '\n')
  4825.         return 1
  4826.     head, tail = path.split(filename)
  4827.     tempname = path.join(head, '@' + tail)
  4828.     tf = None
  4829.     # If we find a match, we rewind the file and start over but
  4830.     # now copy everything to a temp file.
  4831.     while 1:
  4832.         line = f.readline()
  4833.         if not line: break
  4834.         res = findclass(line)
  4835.         if not res:
  4836.             if tf: tf.write(line)
  4837.             continue
  4838.         if not tf:
  4839.             try:
  4840.                 tf = open(tempname, 'w')
  4841.             except IOError, msg:
  4842.                 f.close()
  4843.                 err(tempname+': cannot create: '+`msg`+'\n')
  4844.                 return 1
  4845.             rep(filename + ':\n')
  4846.             # Rewind the input file and start all over:
  4847.             f.seek(0)
  4848.             continue
  4849.         a0, b0 = res[0] # Whole match (up to ':')
  4850.         a1, b1 = res[1] # First subexpression (up to classname)
  4851.         a2, b2 = res[2] # Second subexpression (=.*)
  4852.         head = line[:b1]
  4853.         tail = line[b0:] # Unmatched rest of line
  4854.         if a2 = b2: # No base classes -- easy case
  4855.             newline = head + ':' + tail
  4856.         else:
  4857.             # Get rid of leading '='
  4858.             basepart = line[a2+1:b2]
  4859.             # Extract list of base expressions
  4860.             bases = string.splitfields(basepart, ',')
  4861.             # Strip trailing '()' from each base expression
  4862.             for i in range(len(bases)):
  4863.                 res = findbase(bases[i])
  4864.                 if res:
  4865.                     (x0, y0), (x1, y1) = res
  4866.                     bases[i] = bases[i][x1:y1]
  4867.             # Join the bases back again and build the new line
  4868.             basepart = string.joinfields(bases, ', ')
  4869.             newline = head + '(' + basepart + '):' + tail
  4870.         rep('< ' + line)
  4871.         rep('> ' + newline)
  4872.         tf.write(newline)
  4873.     f.close()
  4874.     if not tf: return 0 # No changes
  4875.     
  4876.     # Finishing touch -- move files
  4877.  
  4878.     # First copy the file's mode to the temp file
  4879.     try:
  4880.         statbuf = posix.stat(filename)
  4881.         posix.chmod(tempname, statbuf[ST_MODE] & 0x7777)
  4882.     except posix.error, msg:
  4883.         err(tempname + ': warning: chmod failed (' + `msg` + ')\n')
  4884.     # Then make a backup of the original file as filename~
  4885.     try:
  4886.         posix.rename(filename, filename + '~')
  4887.     except posix.error, msg:
  4888.         err(filename + ': warning: backup failed (' + `msg` + ')\n')
  4889.     # Now move the temp file to the original file
  4890.     try:
  4891.         posix.rename(tempname, filename)
  4892.     except posix.error, msg:
  4893.         err(filename + ': rename failed (' + `msg` + ')\n')
  4894.         return 1
  4895.     # Return succes
  4896.     return 0
  4897.  
  4898. main()
  4899. 
  4900. 
  4901. Received: by charon.cwi.nl with SMTP; Thu, 26 Dec 1991 13:22:29 +0100
  4902. Received: by voorn.cwi.nl with SMTP; Thu, 26 Dec 1991 12:22:28 GMT
  4903. Message-Id: <9112261222.AA10802@voorn.cwi.nl>
  4904. To: python-list@cwi.nl
  4905. Subject: Bugfix for classfix.py
  4906. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  4907. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  4908. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  4909. Date: Thu, 26 Dec 91 13:22:27 +0100
  4910. Sender: Guido.van.Rossum@cwi.nl
  4911.  
  4912. Oops, the mode in the classfix.py script I just posted to this list was
  4913. masked off with a bogus mask.  Here's a patch:
  4914.  
  4915. 140c140
  4916. <         posix.chmod(tempname, statbuf[ST_MODE] & 0x7777)
  4917. ---
  4918. >         posix.chmod(tempname, statbuf[ST_MODE] & 07777)
  4919.  
  4920. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  4921. "Antidisestablishmentarianism"
  4922. 
  4923. 
  4924. Replied: Sat, 28 Dec 91 17:46:14 +0100
  4925. Replied: Tim Peters <tim@ksr.com>
  4926. Replied: python-list@cwi.nl
  4927. Received: by charon.cwi.nl with SMTP; Sat, 28 Dec 1991 06:03:05 +0100
  4928. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  4929.     (5.61/UUNET-internet-primary) id AA03652; Sat, 28 Dec 91 00:03:03 -0500
  4930. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  4931.     (queueing-rmail) id 000117.25968; Sat, 28 Dec 1991 00:01:17 EST
  4932. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  4933.     id AA18230; Fri, 27 Dec 91 23:45:15 EST
  4934. Received: by kaos.ksr.com (4.0/SMI-3.2)
  4935.     id AA08762; Fri, 27 Dec 91 23:45:14 EST
  4936. Message-Id: <9112280445.AA08762@kaos.ksr.com>
  4937. To: python-list@cwi.nl
  4938. Subject: Class initialization
  4939. Date: Fri, 27 Dec 91 23:45:13 EST
  4940. From: Tim Peters <tim@ksr.com>
  4941.  
  4942. >  [guido, replying to steve m]
  4943. >  ...
  4944. >  Perhaps I could adopt the Modula-3 way (after all I stole enough from
  4945. >  M3 already): a built-in function new() with as its first argument a
  4946. >  class name and as further arguments the arguments to class's init
  4947. >  code.  We also need a way for the user to specify the init code; a
  4948. >  function with a standard name ("init" comes to mind :-) seems fine to
  4949. >  me.
  4950. >  ...
  4951.  
  4952. By sheer coincidence I happened to be thinking about that while waiting
  4953. at an airport today.  Check out the Python function "new" below:
  4954.  
  4955. # module t
  4956.  
  4957. class C0():
  4958.     def init(self):
  4959.         self.att1 = 1
  4960.         self.att2 = 2
  4961.         self.att3 = 3
  4962.         return self
  4963. class C1():
  4964.     def init(self,(att1)):
  4965.         self.att1 = att1
  4966.         self.att2 = 2
  4967.         self.att3 = 3
  4968.         return self
  4969. class C2():
  4970.     def init(self,(att1,att2)):
  4971.         self.att1 = att1
  4972.         self.att2 = att2
  4973.         self.att3 = 3
  4974.         return self
  4975. class C3():
  4976.     def init(self,(att1,att2,att3)):
  4977.         self.att1 = att1
  4978.         self.att2 = att2
  4979.         self.att3 = att3
  4980.         return self
  4981.  
  4982. def new(args):
  4983.     if type(args) = type(('a','dummy','tuple')):
  4984.         Class = args[0]
  4985.         Instance = Class()
  4986.         return Class.init(Instance, args[1:])
  4987.     # just 1 arg, presumably a class
  4988.     return args().init()
  4989.  
  4990. This works pretty well as is (except ... see later):
  4991.  
  4992. >>> from t import *
  4993. >>> def dump(x): print x, x.att1, x.att2, x.att3
  4994. ..
  4995. >>> dump( new(C0) )
  4996. <instance object at 61b90> 1 2 3
  4997. >>> dump( new(C2,[3,4],'cow') )
  4998. <instance object at 60210> [3, 4] cow 3
  4999. >>> dump( new(C3,'a','b',{'c': 1}) )
  5000. <instance object at 60470> a b {'c': 1}
  5001.  
  5002. I don't know why, but this just doesn't work as expected for the C1
  5003. case:
  5004.  
  5005. >>> a = new(C1,0)
  5006. >>> a.att1
  5007. (0,)
  5008. >>>
  5009.  
  5010. I.e., the argument list tuple does not get unpacked as I thought it
  5011. would.  Similarly:
  5012.  
  5013. >>> a,(b) = (1,(2,))  # does not unpack
  5014. >>> a
  5015. 1
  5016. >>> b
  5017. (2,)
  5018. >>> a,(b,) = (1,(2,)) # does unpack
  5019. >>> a
  5020. 1
  5021. >>> b
  5022. 2
  5023. >>>
  5024.  
  5025. But also
  5026.  
  5027. >>> def init(x,(y,)):
  5028. Parsing error: file <stdin>, line 1:
  5029. def init(x,(y,)):
  5030.                ^
  5031. Unhandled exception: run-time error: syntax error
  5032. >>>
  5033.  
  5034. so I can't repair it by changing the declaration of C1's init function
  5035. to make clear that the "2nd argument" really is a singleton tuple.
  5036.  
  5037. Whatever,
  5038.  
  5039. A) My installation of Python is known to suffer from problems that other
  5040.    installations don't have (so don't believe the above unless it
  5041.    happens to you too).
  5042.  
  5043. B) This kind of trick should be easier (more straightforward; more
  5044.    uniform) when the argument-clumping semantics are changed as Guido
  5045.    described in a later mailing.
  5046.  
  5047. C) When a user can program a solution this easily, the case for building
  5048.    it into the language is weak.  To the extent that the case for
  5049.    building it into the language can still be made, I'd be more inclined
  5050.    to see it as pointing at weakness in Python's facilities for
  5051.    manipulating methods & functions in general (as opposed to weakness
  5052.    in Python's class-initialization facilities specifically).  That is,
  5053.    better to go for a general facility than a specific one, and in this
  5054.    case Python is at worst "almost there" already.
  5055.  
  5056. encouraging-y'all-to-think-of-a-built-in-"new"-as-a-personal-defeat<grin>-
  5057.    ly y'rs  - tim
  5058.  
  5059. Tim Peters   Kendall Square Research Corp
  5060. tim@ksr.com,         ksr!tim@uunet.uu.net
  5061. 
  5062. 
  5063. Received: by charon.cwi.nl with SMTP; Sat, 28 Dec 1991 17:46:13 +0100
  5064. Received: by voorn.cwi.nl with SMTP; Sat, 28 Dec 1991 16:46:13 GMT
  5065. Message-Id: <9112281646.AA13805@voorn.cwi.nl>
  5066. To: Tim Peters <tim@ksr.com>
  5067. Cc: python-list@cwi.nl
  5068. Subject: Re: Class initialization 
  5069. In-Reply-To: Your message of "Fri, 27 Dec 91 23:45:13 MET."
  5070.              <9112280445.AA08762@kaos.ksr.com> 
  5071. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  5072. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  5073. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  5074. Date: Sat, 28 Dec 91 17:46:11 +0100
  5075. Sender: Guido.van.Rossum@cwi.nl
  5076.  
  5077. In response to my thinking-aloud about a built-in new() function to
  5078. replace the current class instantiation operation "classname()", Tim
  5079. Peters notices that such a new() function is easily defined in Python,
  5080. goes on to show how, discovers his solution doesn't quite work, and
  5081. wonders why.
  5082.  
  5083. Rest assured, this is not a problem with your version of Python: you
  5084. have just discovered that the obvious way to create a tuple of one
  5085. element (a singleton) doesn't work.  That is:
  5086.  
  5087.     >>> a = (1, 2, 3)
  5088.     >>> a
  5089.     (1, 2, 3)
  5090.     >>> b = (1, 2)
  5091.     >>> b
  5092.     (1, 2)
  5093.     >>>
  5094.  
  5095. but
  5096.  
  5097.     >>> c = (1)
  5098.     >>> c
  5099.     1
  5100.     >>>
  5101.  
  5102. If you think about it, the reason is obvious: parentheses are used for
  5103. grouping, and you certainly don't want (3+4)*5 to construct a tuple
  5104. out of (3+4) -- then the outcome would be (7, 7, 7, 7, 7) instead or 35!
  5105.  
  5106. In early Python there were *no* singleton tuples.  I borrowed tuples
  5107. from ABC, which also has no singleton tuples, but then added tuple
  5108. operations that weren't in ABC, such as subscripting and slicing, and
  5109. it became obvious that singletons were a necessity (they could be
  5110. created by a slice of length one, for instance).  The solution is not
  5111. pretty, but effective: you must write a trailing comma.  With another
  5112. small syntactic hack, an empty pair of parentheses can be used to
  5113. indicate an empty tuple.
  5114.  
  5115. |def new(args):
  5116. |    if type(args) = type(('a','dummy','tuple')):
  5117. |        Class = args[0]
  5118. |        Instance = Class()
  5119. |        return Class.init(Instance, args[1:])
  5120. |    # just 1 arg, presumably a class
  5121. |    return args().init()
  5122. |
  5123. |This works pretty well as is (except ... see later):
  5124.  
  5125. As you noticed, it doesn't work if the class's init function takes
  5126. exactly one argument.  The reason is that your call
  5127. "Class.init(Instance, args[1:])" always passes a tuple.  The proper
  5128. fix is not to change the init function but to make one argument an
  5129. exception as well.  Here's my version:
  5130.  
  5131. def new(args):
  5132.     if type(args) <> type(()):
  5133.         args = (args,) # Tuple-ize
  5134.     Class = args[0]
  5135.     args = args[1:]
  5136.     instance = Class()
  5137.     if len(args) = 0:
  5138.         return instance.init()
  5139.     elif len(args) = 1:
  5140.         return instance.init(args[0])
  5141.     else:
  5142.         return instance.init(args)
  5143.  
  5144. And just to encourage you to go out and fetch the new release, here's
  5145. the version for 0.9.4 -- this is the first case where I found that the
  5146. new bulti-in apply() function is handy:
  5147.  
  5148. def new(args):
  5149.     if type(args) <> type(()):
  5150.         args = (args,) # Tuple-ize
  5151.     Class = args[0]
  5152.     args = args[1:]
  5153.     instance = Class()
  5154.     return apply(instance.init, args)
  5155.  
  5156. I think if you compare the two versions you can see why the new
  5157. parameter handling is better.  The initial "if" is still necessary;
  5158. maybe there should be extra syntax to indicate that the (remaining)
  5159. arguments should be stored as a tuple in the last formal parameter?
  5160.  
  5161. |>>> a,(b) = (1,(2,))  # does not unpack
  5162. |>>> a
  5163. |1
  5164. |>>> b
  5165. |(2,)
  5166. |>>> a,(b,) = (1,(2,)) # does unpack
  5167. |>>> a
  5168. |1
  5169. |>>> b
  5170. |2
  5171.  
  5172. One could make the point that since parenthesized left-hand sides of
  5173. assignments don't make much sense they should always be treated as
  5174. tuples.  However, this would lead to inconsistencies:
  5175.  
  5176.     >>> (a, b, c)    = (1, 2, 3)    # a=1; b=2; c=3
  5177.     >>> (a, b)    = (1, 2)    # a=1; b=2
  5178.     >>> (a)        = (1)        # a=1
  5179.  
  5180. This currently works as expected, but with that change made the last
  5181. statement would be the same error as
  5182.  
  5183.     >>> (a,)    = 1
  5184.  
  5185. is today.
  5186.  
  5187. |>>> def init(x,(y,)):
  5188. |Parsing error: file <stdin>, line 1:
  5189. |def init(x,(y,)):
  5190. |               ^
  5191. |Unhandled exception: run-time error: syntax error
  5192. |>>>
  5193.  
  5194. This was an oversight that will be fixed in version 0.9.5.
  5195.  
  5196. |C) When a user can program a solution this easily, the case for building
  5197. |   it into the language is weak.
  5198.  
  5199. I was proposing it as a *replacement* of the current notation
  5200. "Classname()" (i.e., calling a class as if it were a parameterless
  5201. function).  I've noticed that it is quite a common mistake to write
  5202.  
  5203.     newinstance = Class.init(initialization_arguments)
  5204.  
  5205. instead of
  5206.  
  5207.     newinstance = Class().init(initialization_arguments)
  5208.     #                  ^^
  5209.  
  5210. and the error message that is issued in this case is rather confusing,
  5211. since "Class.init" is a valid function, only it has one more argument
  5212. than is provided...
  5213.  
  5214. I'm not saying that a change is absolutely necessary, but I feel that
  5215. the current solution is overloading function call with something
  5216. rather different.
  5217.  
  5218. An alternative is to encourage a programming style where each class
  5219. defines its own new() function that creates an instance and
  5220. initializes it.  It will still be necessary to separate the init()
  5221. function from the new() function, otherwise derived classes wouldn't
  5222. have a way to call the base class's init() function.
  5223.  
  5224. Anybody else on the list got an opinion?  Should I change the language
  5225. or not?
  5226.  
  5227. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  5228. "That was never five minutes just now!"
  5229. "I'm afraid it was."
  5230. 
  5231. 
  5232. Received: by charon.cwi.nl with SMTP; Sat, 28 Dec 1991 20:12:12 +0100
  5233. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa26405;
  5234.           28 Dec 91 14:11 EST
  5235. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  5236.     id AA06975; Sat, 28 Dec 91 14:03:14 EST
  5237. Date: Sat, 28 Dec 91 14:03:14 EST
  5238. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  5239. Message-Id: <9112281903.AA06975@aemsun.med.Virginia.EDU>
  5240. To: python-list@cwi.nl
  5241. Subject: Syntax for tuples
  5242.  
  5243.  
  5244. I recall reading in *some* language description *somewhere* ( perhaps
  5245.  it was ML ? ) 
  5246. that ( in that language ) the comma operator was considered the tuple 
  5247. constructor, and parentheses were only for grouping and forcing it to 
  5248. be evaluated properly. 
  5249.  
  5250. Python *almost* behaves like this ! 
  5251. The exception being that the emply tuple is written:
  5252.  empty = () 
  5253. rather than: 
  5254.  empty = , 
  5255.  
  5256. ( But if that *was* ML I was recalling, it uses '()' as the empty tuple or 
  5257. UNIT value. ) 
  5258.  
  5259. ( I don't really know ML, but I'm reading (inspired by some of these discussions)
  5260.  David A. Watt's "Programming Language Concepts and Paradigms" which is
  5261.  an excellent "sequal" to Bob Tennent's "Programming Language Principles" 
  5262.  and which ( Wat's book ) uses ML as one of the example languages. ) 
  5263.  
  5264. I *DO* appreciate the argument passing changes - as previously noted, the
  5265. code to handle the exceptions required for thezero/one/many functions
  5266. were an anoyance ( and I had more trouble getting it right for object
  5267. methods ! ) 
  5268.  
  5269. - Steve Majewski 
  5270. 
  5271. 
  5272. Received: by charon.cwi.nl with SMTP; Sun, 29 Dec 1991 11:02:12 +0100
  5273. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  5274.     (5.61/UUNET-internet-primary) id AA18687; Sun, 29 Dec 91 05:02:12 -0500
  5275. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  5276.     (queueing-rmail) id 050116.14399; Sun, 29 Dec 1991 05:01:16 EST
  5277. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  5278.     id AA23102; Sun, 29 Dec 91 04:36:57 EST
  5279. Received: by kaos.ksr.com (4.0/SMI-3.2)
  5280.     id AA04566; Sun, 29 Dec 91 04:36:56 EST
  5281. Message-Id: <9112290936.AA04566@kaos.ksr.com>
  5282. To: guido@cwi.nl
  5283. Subject: Re: Python 0.9.4alpha under the Christmas tree!
  5284. Cc: python-list@cwi.nl
  5285. Date: Sun, 29 Dec 91 04:36:55 EST
  5286. From: Tim Peters <tim@ksr.com>
  5287.  
  5288. Just FYI:
  5289.  
  5290. >  ...
  5291. >  I've uploaded it [python0.9.4alpha.tar.Z] to the following three
  5292. >  places:
  5293. >
  5294. >      host            directory
  5295. >
  5296. >      uunet.uu.net        /tmp
  5297. >      wuarchive.wustl.edu    /pub
  5298. >      ftp.cwi.nl        /pub
  5299.  
  5300. Has not shown up yet on (or has already been deleted from) /tmp on
  5301. uunet.uu.net.
  5302.  
  5303. Is in wuarchive.wustl.edu's /pub, but is missing src/graminit.h.  Not
  5304. sure what I did <grin>, but appeared to successfully worm around that by
  5305. uncommenting the graminit rule at the end of the Makefile, doing
  5306.  
  5307.     make graminit.h
  5308.  
  5309. and then doing a plain "make".
  5310.  
  5311. in-any-case-the-best-christmas-present-i-got-today!-ly y'rs  - tim
  5312.  
  5313. Tim Peters   Kendall Square Research Corp
  5314. tim@ksr.com,         ksr!tim@uunet.uu.net
  5315. 
  5316. 
  5317. Replied: Mon, 30 Dec 91 11:25:06 +0100
  5318. Replied: Tim Peters <tim@ksr.com>
  5319. Replied: python-list@cwi.nl
  5320. Received: by charon.cwi.nl with SMTP; Mon, 30 Dec 1991 06:32:12 +0100
  5321. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  5322.     (5.61/UUNET-internet-primary) id AA18718; Mon, 30 Dec 91 00:32:08 -0500
  5323. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  5324.     (queueing-rmail) id 003121.21874; Mon, 30 Dec 1991 00:31:21 EST
  5325. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  5326.     id AA26322; Mon, 30 Dec 91 00:13:22 EST
  5327. Received: by kaos.ksr.com (4.0/SMI-3.2)
  5328.     id AA25152; Mon, 30 Dec 91 00:13:21 EST
  5329. Message-Id: <9112300513.AA25152@kaos.ksr.com>
  5330. To: guido@cwi.nl
  5331. Subject: Re: Python 0.9.4alpha; Re: Class initialization
  5332. Cc: python-list@cwi.nl
  5333. Date: Mon, 30 Dec 91 00:13:20 EST
  5334. From: Tim Peters <tim@ksr.com>
  5335.  
  5336. Congratulations on 0.9.4alpha -- as usual, a very fine piece of work!
  5337. Some comments:
  5338.  
  5339. >  ...
  5340. >  New argument passing semantics
  5341. >  ...
  5342. >  Two other rules and a new built-in function handle conversion between
  5343. >  tuples and argument lists:
  5344.  
  5345. The "new built-in function" was not mentioned again in this msg; from
  5346. the later "Class initialization" mail I assume it's the new "apply"
  5347. built-in.
  5348.  
  5349. >  Rule (a): when a function with more than one argument is called with a
  5350. >  single argument that is a tuple of the right size, the tuple's items
  5351. >  are used as arguments.
  5352. >
  5353. >  Rule (b): when a function with exactly one argument receives no
  5354. >  arguments or more than one, that one argument will receive a tuple
  5355. >  containing the arguments (the tuple will be empty if there were no
  5356. >  arguments).
  5357.  
  5358. Seems to me that Python must have these rules in order to preserve the
  5359. equivalence (described in misc/CLASSES) between argument-passing
  5360. semantics and the semantics of multiple assignment.  I'm all in favor of
  5361. that!
  5362.  
  5363. But there are still (at least) two things about variadic functions that
  5364. people are certain to trip over (not claiming you don't already realize
  5365. this, am claiming I'm not the only one who will notice <grin>):
  5366.  
  5367. Oddity #1:
  5368.  
  5369. >>> def f(x): print x
  5370. ..
  5371. >>> f(1,2,3)
  5372. (1, 2, 3)
  5373. >>> f(1,2)
  5374. (1, 2)
  5375. >>> f(1)
  5376. 1
  5377. >>> f()
  5378. ()
  5379. >>>
  5380.  
  5381. That is, passing exactly one argument doesn't fit the pattern:  the
  5382. called function does not see a tuple in that (& only in that) case.  I
  5383. do *not* want to see that changed, though, because of the useful analogy
  5384. with the meanings of
  5385.  
  5386.     X = 1, 2, 3  # X is a 3-tuple
  5387.     X = 1, 2     # X is a 2-tuple
  5388.     X = 1         # X is *not* a tuple
  5389.     X = ()         # X is an empty tuple
  5390.  
  5391. More on this a bit later.
  5392.  
  5393. Oddity #2:
  5394.  
  5395. >>> def f(x): print x
  5396. ..
  5397. >>> f()
  5398. ()
  5399. >>> f(())
  5400. ()
  5401. >>>
  5402.  
  5403. That is, a variadic function can't distinguish between being called with
  5404. no arguments and being called with exactly one argument that happens to
  5405. be an empty tuple.  Believe it or not <grin>, I can picture situations
  5406. in which this will create a problem; more later.
  5407.  
  5408. >  ...
  5409. >  - for sequences (string/tuple/list), x[-i] is now equivalent to x[len(x)-i]
  5410.  
  5411. Spooky -- you must have read my mind <0.9 grin>.
  5412.  
  5413. >  ...
  5414. >  - C shifting and masking operators: << >> ~ & ^ | (for ints and longs).
  5415.  
  5416. Noted that (the new?) doc/ref.tex says that the operands must be short
  5417. integers for all of these operators except for ~.  Noted too that Python
  5418. actually accepts longs for all these, so assume your note above is the
  5419. real intent.
  5420.  
  5421. Want to argue that for longs, the best way to specify these operators is
  5422. to (1) treat longs "as if" they were 2's-complement integers with an
  5423. unbounded number of sign bits, and (2) that ">>" be arithmetic when
  5424. applied to longs (that is, since the string of sign bits is conceptually
  5425. infinite, a zero-fill right shift on longs doesn't make sense (as there
  5426. is no "left end" for the zero bits to enter)).  If those rules don't
  5427. appeal to you instantly, happy to try to justify them.
  5428.  
  5429. Python's first cut at this seems rather schizophrenic:
  5430.  
  5431. >>> m1 = -1L
  5432. >>> m1
  5433. -1L
  5434. >>> m1a = m1 >> 1  # expect -1L, mostly because nothing else makes sense
  5435. >>> m1a           # but prints ~0L
  5436. ~0L
  5437.  
  5438. If longs are modeled as unbounded 2's-comp integers, -1L and ~0L are
  5439. both infinite strings of 1's, so I would expect them to be equal:
  5440.  
  5441. >>> (m1 = m1a)  # but they compare unequal
  5442. 0
  5443. >>> (m1 = 0), (m1a = 0)  # Python thinks ~0L is equal to 0 (!)
  5444. (0, 1)
  5445.  
  5446. In some respects m1a *is* acting like 0, but in others it's acting like
  5447. an infinite string of 1 bits, and in others ...:
  5448.  
  5449. >>> 42 + m1a  # acts like 0 here
  5450. 42L
  5451. >>> 42 & m1a  # acts like binary ...1111...111 here
  5452. 42L
  5453. >>> 42 | m1a  # not sure what to make of this <0.9 grin>
  5454. -42L
  5455. >>> 42 * m1a  # acts like 0 here
  5456. 0L
  5457. >>> 42 ^ m1a  # curiouser & curiouser ...
  5458. 32725L
  5459.  
  5460. It is a bit tricky, but neither truly hard nor inefficient, to create
  5461. the consistent illusion of an infinite string of sign bits; if you agree
  5462. it's a good idea, I'd be happy to help with the algorithms.
  5463.  
  5464. >  ... [skipping to msg "Re: Class initialization"] ...
  5465. >  ... Tim notices that such a new() function is easily defined in
  5466. >  Python, goes on to show how, discovers his solution doesn't quite
  5467. >  work, and wonders why.
  5468. >
  5469. >  Rest assured, this is not a problem with your version of Python: you
  5470. >  have just discovered that the obvious way to create a tuple of one
  5471. >  element (a singleton) doesn't work.
  5472.  
  5473. Ha -- you have done me the favor of underestimating my ignorance <smile>.
  5474. Turns out that what I actually didn't understand is that when a method
  5475. was invoked with one argument
  5476.  
  5477.     instance.method(an_argument)
  5478.  
  5479. then "method" saw exactly two arguments: the instance object, and
  5480. an_argument.  I erroneously *assumed* it would see the instance object
  5481. and a singleton *tuple* containing an_argument (in analogy with how,
  5482. e.g., after
  5483.     instance.method(arg1,arg2)
  5484. "method" saw an instance argument and a 2-tuple (arg1,arg2)).  From
  5485. there I went on to believe that parentheses in argument lists actually
  5486. did force tuple unpacking, and ... garbage in, garbage out.  All your
  5487. explanations made sense here, and I'm darned glad you changed the
  5488. argument-passing method anyway <grin>.
  5489.  
  5490. >  ... [a cleaner "new" that works under the new rules] ...
  5491. >  def new(args):
  5492. >      if type(args) <> type(()):
  5493. >          args = (args,) # Tuple-ize
  5494. >      Class = args[0]
  5495. >      args = args[1:]
  5496. >      instance = Class()
  5497. >      return apply(instance.init, args)
  5498. >
  5499. >  I think if you compare the two versions you can see why the new
  5500. >  parameter handling is better.  The initial "if" is still necessary;
  5501. >  maybe there should be extra syntax to indicate that the (remaining)
  5502. >  arguments should be stored as a tuple in the last formal parameter?
  5503.  
  5504. I would like that.  As noted above, the current scheme still contains
  5505. some traps for the unwary (one of which is again illustrated in your
  5506. "new").  In addition, I suspect that variadic functions are rare enough
  5507. that it would help readability/maintenance if the declaration of a
  5508. variadic function had a bit of extra syntactic fluff to make it obvious.
  5509.  
  5510. How about stealing Icon's gimmick for this?:
  5511.  
  5512.     def f(args()): ...
  5513.           ^^
  5514.  
  5515. The trailing parens here have no meaning in Python now (it's currently a
  5516. syntax error), and is meant to say "the arguments (if any) to f are to
  5517. be placed in a tuple and stored in 'args'; that tuple will be empty iff
  5518. f is invoked without arguments".
  5519.  
  5520. Thus
  5521.     f()    sets args to the empty tuple ()
  5522.     f(())    sets args to the singleton tuple ((),)
  5523.     f(1)    sets args to the singleton tuple (1,)
  5524.     f(1,2)    sets args to the 2-tuple (1,2)
  5525.     etc
  5526.  
  5527. This is clean & I believe it's surprise-free (at least I've never been
  5528. surprised by it when using Icon's equivalent ...).
  5529.  
  5530. Icon actually goes a bit farther and allows any number of "plain"
  5531. arguments to precede the (optional) catch-the-rest argument.  E.g.,
  5532.  
  5533.     def msg(outfile, tag, strings()):
  5534.         outfile.write( tag )
  5535.         if len(strings) > 0: outfile.write( ': ' )
  5536.         for s in strings: outfile.write( s )
  5537.         outfile.write( '\n' )
  5538.  
  5539. might be used to define a variadic msg-printer.
  5540.  
  5541. The one thing that bothers me about this is weakening the link between
  5542. argument-passing and assignment semantics; but perhaps this kind of
  5543. notation would also be useful in assignments?  E.g.,
  5544.  
  5545.     head, A() = A  # put A[0] in head, & A[1:] in A
  5546.  
  5547. There are probably surprises lurking in that, though -- my head is
  5548. swimming already <0.9 grin>.  Comments?
  5549.  
  5550. >  ...
  5551. >  |C) When a user can program a solution this easily, the case for building
  5552. >  |   it into the language is weak.
  5553. >
  5554. >  I was proposing it as a *replacement* of the current notation
  5555. >  "Classname()" (i.e., calling a class as if it were a parameterless
  5556. >  function).  I've noticed that it is quite a common mistake to write
  5557. >
  5558. >      newinstance = Class.init(initialization_arguments)
  5559. >
  5560. >  instead of
  5561. >
  5562. >      newinstance = Class().init(initialization_arguments)
  5563. >      #                  ^^
  5564. >
  5565. >  and the error message that is issued in this case is rather confusing,
  5566. >  since "Class.init" is a valid function, only it has one more argument
  5567. >  than is provided...
  5568. >
  5569. >  I'm not saying that a change is absolutely necessary, but I feel that
  5570. >  the current solution is overloading function call with something
  5571. >  rather different.
  5572.  
  5573. Suggest that the case for making a change got much stronger when the
  5574. class syntax changed; that is, now that a class is declared via (e.g.)
  5575.  
  5576.     class Derived(Base):
  5577.  
  5578. it is much more tempting to fall into the mistake of believing that
  5579.  
  5580.     Derived()
  5581.  
  5582. means something it doesn't ("hmm, it must be passing an empty tuple
  5583. to the 'Base' argument!").
  5584.  
  5585. >  ...
  5586. >  Anybody else on the list got an opinion?  Should I change the language
  5587. >  or not?
  5588.  
  5589. Think people need a concrete alternative to pick on.  Since I'm on
  5590. vacation this week, I'll volunteer to be the flame absorber <0.9 grin>:
  5591.  
  5592.   - The "Class()" notation goes away; you can no longer create a class
  5593.     instance that way, & it's an error to try to.  However, class
  5594.     attributes continue to work exactly as they work now.
  5595.  
  5596.   - A built-in "new(Class_name, optional_arguments)" is introduced for
  5597.     creating class instances.  An instance of class Class_name will be
  5598.     created.  If the instance does not have a method named (exactly)
  5599.     "init", the instance is returned and it is an error if any optional
  5600.     arguments were specified.  Else the instance's init method is
  5601.     invoked with an argument list composed of the instance and the
  5602.     optional arguments (if any) passed to "new", and the result of the
  5603.     init method is returned by "new".
  5604.  
  5605.   - There is nothing special or unusual about a method named "init"
  5606.     except that "new" looks for a method with that name.
  5607.  
  5608.   - You want more than that, program it yourself <grin>.
  5609.  
  5610. fast-running-out-of-things-to-whine-about-ly y'rs  - tim
  5611.  
  5612. Tim Peters   Kendall Square Research Corp
  5613. tim@ksr.com,         ksr!tim@uunet.uu.net
  5614. 
  5615. 
  5616. To: Tim Peters <tim@ksr.com>
  5617. cc: python-list@cwi.nl
  5618. Subject: Re: Python 0.9.4alpha; Re: Class initialization 
  5619. In-reply-to: Your message of "Mon, 30 Dec 91 00:13:20 MET."
  5620.              <9112300513.AA25152@kaos.ksr.com> 
  5621. From: Guido van Rossum <guido@cwi.nl>
  5622. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  5623. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  5624. Date: Mon, 30 Dec 91 11:25:03 +0100
  5625. Sender: guido
  5626.  
  5627. Tim,
  5628.  
  5629. Sorry, I don't have the time to respond to all that (will do later),
  5630. but here are a few quick remarks about shifting and masking on long ints.
  5631.  
  5632. First, long ints have changes to a *one's complement* representation.
  5633. This was the only way that I could give ~0L the meaning of "all 1 bits".
  5634. Of course, as soon as you use any arithmetic on it, it changes to 0L,
  5635. and arithmetic operations (including unary "-") will never produce it.
  5636.  
  5637. Second, there's a bug in the implementation of long &, | and ^ when
  5638. one operand is negative and larger than the other; e.g., the outcome
  5639. for "1L & ~1000000L" is wrong.  Fixed in 0.9.5.
  5640.  
  5641. I hope this explains most of what you've discovered.
  5642.  
  5643. "I told you the documentation was out of date :-)"
  5644.  
  5645. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  5646. "What a senseless waste of human life"
  5647. 
  5648. 
  5649. Received: by charon.cwi.nl with SMTP; Mon, 30 Dec 1991 11:25:05 +0100
  5650. Received: by voorn.cwi.nl with SMTP; Mon, 30 Dec 1991 10:25:04 GMT
  5651. Message-Id: <9112301025.AA16480@voorn.cwi.nl>
  5652. To: Tim Peters <tim@ksr.com>
  5653. Cc: python-list@cwi.nl
  5654. Subject: Re: Python 0.9.4alpha; Re: Class initialization 
  5655. In-Reply-To: Your message of "Mon, 30 Dec 91 00:13:20 MET."
  5656.              <9112300513.AA25152@kaos.ksr.com> 
  5657. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  5658. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  5659. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  5660. Date: Mon, 30 Dec 91 11:25:03 +0100
  5661. Sender: Guido.van.Rossum@cwi.nl
  5662.  
  5663. Tim,
  5664.  
  5665. Sorry, I don't have the time to respond to all that (will do later),
  5666. but here are a few quick remarks about shifting and masking on long ints.
  5667.  
  5668. First, long ints have changes to a *one's complement* representation.
  5669. This was the only way that I could give ~0L the meaning of "all 1 bits".
  5670. Of course, as soon as you use any arithmetic on it, it changes to 0L,
  5671. and arithmetic operations (including unary "-") will never produce it.
  5672.  
  5673. Second, there's a bug in the implementation of long &, | and ^ when
  5674. one operand is negative and larger than the other; e.g., the outcome
  5675. for "1L & ~1000000L" is wrong.  Fixed in 0.9.5.
  5676.  
  5677. I hope this explains most of what you've discovered.
  5678.  
  5679. "I told you the documentation was out of date :-)"
  5680.  
  5681. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  5682. "What a senseless waste of human life"
  5683. 
  5684. 
  5685. Replied: Tue, 31 Dec 91 12:36:25 +0100
  5686. Replied: Tim Peters <tim@ksr.com>
  5687. Received: by charon.cwi.nl with SMTP; Tue, 31 Dec 1991 06:32:23 +0100
  5688. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  5689.     (5.61/UUNET-internet-primary) id AA00826; Tue, 31 Dec 91 00:32:21 -0500
  5690. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  5691.     (queueing-rmail) id 003123.17340; Tue, 31 Dec 1991 00:31:23 EST
  5692. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  5693.     id AA04475; Tue, 31 Dec 91 00:08:20 EST
  5694. Received: by kaos.ksr.com (4.0/SMI-3.2)
  5695.     id AA03104; Tue, 31 Dec 91 00:08:17 EST
  5696. Message-Id: <9112310508.AA03104@kaos.ksr.com>
  5697. To: guido@cwi.nl
  5698. Subject: Semantics of long ints
  5699. Cc: python-list@cwi.nl
  5700. Date: Tue, 31 Dec 91 00:08:15 EST
  5701. From: Tim Peters <tim@ksr.com>
  5702.  
  5703. >  ... a few quick remarks about shifting and masking on long ints.
  5704. >
  5705. >  First, long ints have changes to a *one's complement* representation.
  5706.  
  5707. That is consistent with some of the samples I posted.
  5708.  
  5709. >  This was the only way that I could give ~0L the meaning of "all 1 bits".
  5710.  
  5711. ?  Don't see a connection.  E.g., it appears that short ints are (as C
  5712. greatly favors) represented in 2's-comp, and on this 32-bit machine I get
  5713.  
  5714. >>> (~0 = 0xffffffff)
  5715. 1
  5716.  
  5717. That is, ~0 means "all 1's" for short 2's-comp ints, so how could it be
  5718. a problem for ~0 to mean "all 1's" for long 2's-comp ints too?  I think
  5719. I must be missing your meaning badly.
  5720.  
  5721. If the underlying question is "if long ints are unbounded 2's-comp
  5722. integers, then how can the ~ operator be implemented such that ~0 works
  5723. out to being all 1's (and in all other respects does 'the right
  5724. thing')?", the tricky-but-simple answer is "in all cases, ~ should add
  5725. 1, then flip the sign bit".  That follows from the 2's-comp identity
  5726.     -x = ~x + 1
  5727. so that
  5728.     ~x = -x - 1 = -(x+1)
  5729.  
  5730. This assumes that *internally* a signed-magnitude representation is used
  5731. for longs (which I believe is true in Python).  Then ~ applied to a 0
  5732. will (1) add 1, yielding 1, & (2) flip the sign bit, yielding -1, the
  5733. correct 2's-comp way to spell "all 1's".  Similarly, ~ applied to -1
  5734. will (1) add 1, yielding 0, & (2) flip the sign bit, again yielding 0,
  5735. the correct 2's-comp way to spell "all 0's".  It's not the least charm
  5736. of 2's comp that these "spellings" are unique ...
  5737.  
  5738.  
  5739. It's clearly easier on the user if short ints & long ints are (at least
  5740. conceptually) represented the same way; since C matches nearly all
  5741. widely used machines in favoring 2's comp, and Python is implemented in
  5742. C, it makes sense for Python to mirror C's rules for short ints.  Using
  5743. a different method for long ints will cause inconsistencies, like the
  5744. current
  5745.  
  5746. >>> 5 & (-1)
  5747. 5
  5748. >>> 5L & (-1L)
  5749. 4L
  5750. >>>
  5751.  
  5752. That's not good; e.g., consider the poor user who tries to boost the
  5753. precision in their program by switching to long arithmetic and gets
  5754. different answers as a result.
  5755.  
  5756. >  Of course, as soon as you use any arithmetic on it, it changes to 0L,
  5757.  
  5758. Ya, another reason to hate 1's comp <grin>.
  5759.  
  5760. >  and arithmetic operations (including unary "-") will never produce it.
  5761. >
  5762. >  Second, there's a bug in the implementation of long &, | and ^ when
  5763. >  one operand is negative and larger than the other; e.g., the outcome
  5764. >  for "1L & ~1000000L" is wrong.  Fixed in 0.9.5.
  5765. >
  5766. >  I hope this explains most of what you've discovered.
  5767.  
  5768. Let's see ... The "~0L acts like 0 in arithmetic" explains the results
  5769. for the "+" and "*" examples.  The "~0L acts like all 1's" explains the
  5770. result for the "&" example.  Believe the "|" and "^" examples remain
  5771. mysteries (those don't appear to fit the "one operand is negative and
  5772. larger than the other" clause):
  5773.  
  5774. >>> 42L | (~0L)
  5775. -42L
  5776. >>> 42L ^ (~0L)
  5777. 32725L
  5778.  
  5779. Oops, add one more to the pot:
  5780.  
  5781. >>> (~0L) << 1  # expected -1L
  5782. ~0L
  5783.  
  5784. >  "I told you the documentation was out of date :-)"
  5785.  
  5786. Na, documentation is for wimps -- I wouldn't read it anyway <grin>.
  5787.  
  5788. bigints-are-such-a-tricky-business-almost-nobody-gets-them-right-ly y'rs
  5789.    - tim
  5790.  
  5791. Tim Peters   Kendall Square Research Corp
  5792. tim@ksr.com,         ksr!tim@uunet.uu.net
  5793. 
  5794. 
  5795. Received: by charon.cwi.nl with SMTP; Tue, 31 Dec 1991 15:17:40 +0100
  5796. Received: by schelvis.cwi.nl with SMTP; Tue, 31 Dec 1991 14:17:39 GMT
  5797. Message-Id: <9112311417.AA12809@schelvis.cwi.nl>
  5798. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  5799. Cc: python-list@cwi.nl
  5800. Subject: Re: Steve Majewski's mail 
  5801. In-Reply-To: Message by Guido van Rossum <Guido.van.Rossum@cwi.nl> ,
  5802.          Tue, 24 Dec 91 12:02:43 +0100 , <9112241102.AA05630@voorn.cwi.nl> 
  5803. Organisation: AMOEBA project, CWI, Kruislaan 413, Amsterdam
  5804. Phone: +31 20 5924098(work), +31 20 6160335(home)
  5805. X-Last-Band-Seen: Urban Dance Squad (Paradiso), Nozems (Korsakoff, 19-12)
  5806. X-Mini-Review: Two *great* gigs in one evening!
  5807. Date: Tue, 31 Dec 91 15:17:38 +0100
  5808. From: Jack.Jansen@cwi.nl
  5809.  
  5810. On the subject of ignoring the object-orientedness in the tutorial:
  5811. I don't think this is a good idea. Even a short note about the
  5812. reference/value issues (and probably a line saying that the details will
  5813. be expained lateron) somewhere early in the manual would have saved
  5814. me quite a bit of grief in my first experiences with python.
  5815. --
  5816. Jack Jansen        | In Holland things are serious, but never hopeless.
  5817. Jack.Jansen@cwi.nl | In Ireland things are hopeless, but never serious.
  5818. uunet!cwi.nl!jack   G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  5819. 
  5820. 
  5821. Received: by charon.cwi.nl with SMTP; Thu, 2 Jan 1992 11:37:24 +0100
  5822. X400-Received: by mta relay.surfnet.nl in /PRMD=surf/ADMD=400net/C=nl/; Relayed;
  5823.                Thu, 2 Jan 1992 11:36:57 +0100
  5824. X400-Received: by /PRMD=UK.AC/ADMD= /C=GB/; Relayed;
  5825.                Thu, 2 Jan 1992 11:36:18 +0100
  5826. X400-Received: by /PRMD=UK.AC/ADMD= /C=GB/; Relayed;
  5827.                Thu, 2 Jan 1992 11:35:50 +0100
  5828. Date: Thu, 2 Jan 1992 10:35:50 +0000
  5829. X400-Originator: MAILER-DAEMON@syma.sussex.ac.uk
  5830. X400-Mts-Identifier: [/PRMD=UK.AC/ADMD= /C=GB/;<1255.9201021035@syma.sussex.ac.]
  5831. X400-Content-Type: P2-1984 (2)
  5832. Content-Identifier: Returned mail...
  5833. From: Mail Delivery Subsystem <MAILER-DAEMON@syma.sussex.ac.uk>
  5834. Message-Id: <1255.9201021035@syma.sussex.ac.uk>
  5835. To: guido@cwi.nl
  5836. Subject: Returned mail: Cannot send message for 3 days
  5837.  
  5838.    ----- Transcript of session follows -----
  5839. 421 eaps-mail (ether)... Deferred: Network is unreachable
  5840.  
  5841.    ----- Unsent message follows -----
  5842. Received: from sun4nl.nluug.nl by eros.uknet.ac.uk via EUnet with SMTP (PP) 
  5843.           id <29350-0@eros.uknet.ac.uk>; Mon, 30 Dec 1991 10:28:10 +0000
  5844. Received: from charon.cwi.nl by sun4nl.nluug.nl with SMTP;
  5845.           id AA23632 (5.65a/CWI-3.1); Mon, 30 Dec 1991 11:27:12 +0100
  5846. Received: by charon.cwi.nl with SMTP; Mon, 30 Dec 1991 11:25:05 +0100
  5847. Received: by voorn.cwi.nl with SMTP; Mon, 30 Dec 1991 10:25:04 GMT
  5848. Message-Id: <9112301025.AA16480@voorn.cwi.nl>
  5849. To: Tim Peters <tim@ksr.com>
  5850. Cc: python-list@cwi.nl
  5851. Subject: Re: Python 0.9.4alpha; Re: Class initialization
  5852. In-Reply-To: Your message of "Mon, 30 Dec 91 00:13:20 MET." <9112300513.AA25152@kaos.ksr.com>
  5853. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  5854. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  5855. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  5856. Date: Mon, 30 Dec 91 11:25:03 +0100
  5857. Original-Sender: Guido.van.Rossum@nl.cwi
  5858. Sender: guido@cwi.nl
  5859.  
  5860. Tim,
  5861.  
  5862. Sorry, I don't have the time to respond to all that (will do later),
  5863. but here are a few quick remarks about shifting and masking on long ints.
  5864.  
  5865. First, long ints have changes to a *one's complement* representation.
  5866. This was the only way that I could give ~0L the meaning of "all 1 bits".
  5867. Of course, as soon as you use any arithmetic on it, it changes to 0L,
  5868. and arithmetic operations (including unary "-") will never produce it.
  5869.  
  5870. Second, there's a bug in the implementation of long &, | and ^ when
  5871. one operand is negative and larger than the other; e.g., the outcome
  5872. for "1L & ~1000000L" is wrong.  Fixed in 0.9.5.
  5873.  
  5874. I hope this explains most of what you've discovered.
  5875.  
  5876. "I told you the documentation was out of date :-)"
  5877.  
  5878. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  5879. "What a senseless waste of human life"
  5880. 
  5881. 
  5882. To: hhg1@gte.com (Hallett German)
  5883. Subject: Re: Mac Binary of Python 0.94? 
  5884. In-reply-to: Your message of "Tue, 24 Dec 91 07:39:32 MET."
  5885.              <9112241239.AA15186@bunny.gte.com> 
  5886. From: Guido van Rossum <guido@cwi.nl>
  5887. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  5888. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  5889. Date: Thu, 02 Jan 92 13:19:56 +0100
  5890. Sender: guido
  5891.  
  5892. >Does this bird exist? Thanks for the Xmas present!
  5893.  
  5894. Would you settle for a Mac binary of a 0.9.5 pre-release?  From the README file:
  5895.  
  5896. - dictionary objects can now be compared properly; e.g., {}=={} is true
  5897. - '==' is now the only equality operator; see demo/scripts/eqfix.py
  5898.   for a script that fixes old Python modules
  5899. - new exception SystemExit causes termination if not caught;
  5900.   it is raised by sys.exit() so that 'finally' clauses can clean up,
  5901.   and it may even be caught.  It does work interactively!
  5902. - new module "regex" interfaces to GNU Emacs regular expression;
  5903.   module "regexp" rewritten in Python for backward compatibility
  5904. - formal parameter lists may contain trailing commas
  5905.  
  5906. I'll release the sources for 0.9.5 soon (still no up-to-date manual, but I'm
  5907. working on it) so I think you'll be better off with a Mac version of that
  5908. release anyway.
  5909.  
  5910. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  5911. "It's a bit runny, sir"
  5912. 
  5913. 
  5914. Received: by charon.cwi.nl with SMTP; Thu, 2 Jan 1992 21:22:25 +0100
  5915. Received: by voorn.cwi.nl with SMTP; Thu, 2 Jan 1992 20:22:24 GMT
  5916. Message-Id: <9201022022.AA01020@voorn.cwi.nl>
  5917. To: hhg1@gte.com (Hallett German)
  5918. Cc: python-list@cwi.nl
  5919. Subject: 0.9.5 for the Mac. 
  5920. In-Reply-To: Your message of "Thu, 02 Jan 92 15:13:01 MET."
  5921.              <9201022013.AA14459@bunny.gte.com> 
  5922. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  5923. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  5924. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  5925. Date: Thu, 02 Jan 92 21:22:23 +0100
  5926. Sender: Guido.van.Rossum@cwi.nl
  5927.  
  5928. For those who wish to try Python on the Mac, I've made a pre-release
  5929. version of 0.9.5 for the Mac available by anonymous ftp from site
  5930. ftp.cwi.nl, file /pub/Python0.9.5.hqx.  This is just an application
  5931. binary; you will still have to fetch UNIX 0.9.4 distribution to get
  5932. the library modules.
  5933.  
  5934. Here's the list of changes in 0.9.5 compared to 0.9.4, insofar as they
  5935. are relevant to the Mac version:
  5936.  
  5937. Features:
  5938. - dictionary objects can now be compared properly; e.g., {}=={} is true
  5939. - new exception SystemExit causes termination if not caught;
  5940.   it is raised by sys.exit() so that 'finally' clauses can clean up,
  5941.   and it may even be caught.  It does work interactively!
  5942. - new module "regex" interfaces to GNU Emacs regular expression;
  5943.   old module "regexp" rewritten in Python for backward compatibility
  5944. - formal parameter lists may contain trailing commas
  5945.  
  5946. Bugs fixed:
  5947. - assigning to or deleting a list item with a negative index dumped core
  5948. - masking operations involving negative long integers gave wrong results
  5949.  
  5950. UNIX source will be made available soon.
  5951. 
  5952. 
  5953. Received: by charon.cwi.nl with SMTP; Thu, 2 Jan 1992 23:34:44 +0100
  5954. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa15611;
  5955.           2 Jan 92 17:34 EST
  5956. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  5957.     id AA09844; Thu, 2 Jan 92 17:25:37 EST
  5958. Date: Thu, 2 Jan 92 17:25:37 EST
  5959. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  5960. Message-Id: <9201022225.AA09844@aemsun.med.Virginia.EDU>
  5961. To: Guido.van.Rossum@cwi.nl
  5962. Subject: Re:  0.9.5 for the Mac.
  5963. Cc: tim@ksr.com
  5964.  
  5965. Got it! 
  5966. ( Now I can finally python at home, without using my modem ! ) 
  5967.  
  5968. Now I have problems importing modules.
  5969. I suppose the way around is to rename: 
  5970.   macpath.py  --to--> path.py 
  5971.   maccache.py --to--> dircache.py 
  5972.  ...etc... 
  5973.  
  5974. Is this the 'recommended' way, or should I try to 
  5975. change some of the files to 'conditional' imports ? 
  5976.  
  5977.  
  5978. I am waiting for a new C compiler ( Borland, instead on MS ) to 
  5979. be delivered. I am going to be doing some development work on 
  5980. a PC platform for a change, so I will try to give you a PC port
  5981. of python sometime in the near future. 
  5982.  
  5983. ( Yes, I'm back from vacation, so you will be getting some more 
  5984.   comments on tuples, exceptions, etc. as soon as I have digested
  5985.   the new documentation and caught up with Tim & your messages. )
  5986.  
  5987.  
  5988. BTW: 
  5989.  (almost) everything that Tim said in his last series of messages 
  5990.  goes for me also. I always feel the need to issue some disclaimers
  5991.  before writing requests/suggestions/etc. Some are obviously meant 
  5992.  to make python easier to learn/use ... others are in the aesthetic
  5993.  or blue-sky realm ( where opinions often differ ) and an appropriate
  5994.  response may be: "Steve, I'll be very interested to see how YOU 
  5995.  resolve that question when you implement it in STEVE-TRAN/STEVE-BOL/or
  5996.  whatever!" :-) 
  5997.  
  5998.  Please take all the comments as a compliment on the design of python: 
  5999.  I have found it both practically useful and theoretically interesting.
  6000.  
  6001.  - Steve 
  6002. 
  6003. 
  6004. Replied: Mon, 06 Jan 92 19:32:34 +0100
  6005. Replied: python-list@cwi.nl
  6006. Received: by charon.cwi.nl with SMTP; Mon, 6 Jan 1992 17:55:27 +0100
  6007. Received: by meermin.cwi.nl with SMTP; Sat, 4 Jan 1992 02:56:21 GMT
  6008. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa02454;
  6009.           3 Jan 92 13:19 EST
  6010. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  6011.     id AA10409; Fri, 3 Jan 92 13:10:44 EST
  6012. Date: Fri, 3 Jan 92 13:10:44 EST
  6013. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  6014. Message-Id: <9201031810.AA10409@aemsun.med.Virginia.EDU>
  6015. To: python-list@cwi.nl
  6016. Subject: errors building 0.9.4alpha 
  6017.  
  6018. In makeing Python 0.9.4 alpha, I get errors on the case statements
  6019. in compile.c :
  6020.  
  6021. /*###1200 [cc] xor_expr undefined%%%*/
  6022. /*###1200 [cc] case expression must be an integer constant%%%*/
  6023. /*###1201 [cc] case expression must be an integer constant%%%*/
  6024. /*###1201 [cc] and_expr undefined%%%*/
  6025. /*###1202 [cc] case expression must be an integer constant%%%*/
  6026. /*###1202 [cc] shift_expr undefined%%%*/
  6027. /*###1203 [cc] case expression must be an integer constant%%%*/
  6028. /*###1203 [cc] arith_expr undefined%%%*/
  6029. /*###1851 [cc] global_stmt undefined%%%*/
  6030. /*###1851 [cc] case expression must be an integer constant%%%*/
  6031. /*###1893 [cc] xor_expr undefined%%%*/
  6032. /*###1893 [cc] case expression must be an integer constant%%%*/
  6033. /*###1896 [cc] case expression must be an integer constant%%%*/
  6034. /*###1896 [cc] and_expr undefined%%%*/
  6035. /*###1899 [cc] case expression must be an integer constant%%%*/
  6036. /*###1899 [cc] shift_expr undefined%%%*/
  6037. /*###1902 [cc] case expression must be an integer constant%%%*/
  6038. /*###1902 [cc] arith_expr undefined%%%*/
  6039.  
  6040.  
  6041. graminit.h  seems to contain some of the other defines but not 
  6042. the above.
  6043.  
  6044. ( grep expr *.h, excluding the uninteresting comment lines ) >>
  6045. graminit.h:#define expr_input 258
  6046. graminit.h:#define expr_stmt 267
  6047. graminit.h:#define expr 289
  6048. graminit.h:#define exprlist 295
  6049.  
  6050.  
  6051. Anyone else run into this problem ? 
  6052.  
  6053. - Steve Majewski
  6054. 
  6055. 
  6056. Received: by charon.cwi.nl with SMTP; Mon, 6 Jan 1992 19:32:35 +0100
  6057. Received: by voorn.cwi.nl with SMTP; Mon, 6 Jan 1992 18:32:33 GMT
  6058. Message-Id: <9201061832.AA09824@voorn.cwi.nl>
  6059. To: python-list@cwi.nl
  6060. Subject: Re: errors building 0.9.4alpha 
  6061. In-Reply-To: Your message of "Fri, 03 Jan 92 13:10:44 MET."
  6062.              <9201031810.AA10409@aemsun.med.Virginia.EDU> 
  6063. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  6064. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6065. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6066. Date: Mon, 06 Jan 92 19:32:32 +0100
  6067. Sender: Guido.van.Rossum@cwi.nl
  6068.  
  6069. Steven Majewski writes:
  6070.  
  6071. >In makeing Python 0.9.4 alpha, I get errors on the case statements
  6072.     ^^^^^^^[sic]
  6073. >in compile.c :
  6074. >
  6075. >/*###1200 [cc] xor_expr undefined%%%*/
  6076. >/*###1200 [cc] case expression must be an integer constant%%%*/
  6077. [and lots similar]
  6078.  
  6079. I bet you extracted 0.9.4 in the same location where 0.9.2 was.
  6080.  
  6081. 0.9.4 was accidentally distributed without "graminit.[ch]" files,
  6082. which I generate lcally whenever I change the syntax, but which the
  6083. distributed Makefile believes are part of the distribution.  (Of
  6084. course I use my own version of the Makefile :-)
  6085.  
  6086. The cure is to get rid of the graminit.[ch] you have and uncomment the
  6087. last two non-blank lines of the Makefile.  The next "make" will now
  6088. build "pythongen" and run it with "Grammar" as input, producing
  6089. correct versions (I sincerely expect and hope).
  6090.  
  6091. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl> Honary Member, Royal
  6092. Society for Putting Things on Top of Other Things
  6093. 
  6094. 
  6095. Received: by charon.cwi.nl with SMTP; Wed, 15 Jan 1992 14:01:58 +0100
  6096. Received: by voorn.cwi.nl with SMTP; Wed, 15 Jan 1992 13:01:57 GMT
  6097. Message-Id: <9201151301.AA05944@voorn.cwi.nl>
  6098. To: python-list@cwi.nl
  6099. Subject: Why some things are the way they are in Python
  6100. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  6101. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6102. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6103. Date: Wed, 15 Jan 92 14:01:56 +0100
  6104. Sender: Guido.van.Rossum@cwi.nl
  6105.  
  6106. In the past few weeks I've had a lot of off-line discussions about
  6107. various issues of Python's design with Tim Peters and Steven Majewski.
  6108. Longer ago I had similar e-mail exchanges with Daniel LaLiberte.
  6109.  
  6110. There was a discussion of variable length argument lists from which I
  6111. still have to recover (more about that another time), but there were
  6112. also a whole lot of questions raised about other parts of Python's
  6113. design.  Rather than repeat my responses over and over again each time
  6114. someone raises one such issues again, I've written a little "Socratic"
  6115. dialogue that tries to explain why things are the way they are.
  6116.  
  6117. The main subjects treated in this dialogue are, roughly:
  6118.  
  6119. - why Python has both (immutable) tuples and (mutable) lists
  6120. - the rationale behind singleton and empty tuples
  6121. - why the parentheses in a function call can't be made optional
  6122.  
  6123. It also explains why 'print' is a statement and not a function, and
  6124. gives some examples of what you can do with None.
  6125.  
  6126.  
  6127. <Q> Why does Python have both tuples and lists?
  6128.  
  6129. <A> They serve different purposes.  Lists can get quite long, they are
  6130. generally built incrementally, and therefore have to be mutable.
  6131. Tuples on the other hand are generally short and created at once.
  6132.  
  6133. <Q> Then why can't tuples be mutable and serve both purposes?
  6134.  
  6135. <A> Imagine a graphics class that stores coordinates of graphical
  6136. objects as tuples.  It may also store rectangles as a tuple of points,
  6137. etc.  If tuples were mutable, the class would have to store copies of
  6138. all points and rectangles it receives, since otherwise a caller who
  6139. creates a point variable, passes its value to the graphics class, and
  6140. later changes the point for its own use (e.g., to create another
  6141. graphical object with slightly different coordinates) might violate
  6142. the internal consistency of the graphics class.  Note that most
  6143. callers woouldn't modify the points, but the graphics class has no way
  6144. to tell, so it has to make the copies anyway.  (And now imaging the
  6145. software is actually layered in such a way that coordinates are passed
  6146. down several levels deep...)
  6147.  
  6148. <Q> Then why can't lists be made immutable?  There are algorithms and
  6149. data structures that guarantee O(log N) or O(N log N)
  6150. insert/delete/concatenate/access operations, e.g., B-trees.
  6151.  
  6152. <A> This was used in ABC for lists and tables, where I helped
  6153. implement it.  My experiences with this were that the code was
  6154. incredibly complex and thus difficult to maintain; the overhead for
  6155. small lists (which are in the majority in most programs) was
  6156. considerable, and the access time for single elements was O(log N).
  6157.  
  6158. <Q> Why are there singleton and empty tuples at all in Python?
  6159. Wouldn't it be easier to forbid them?  After all special syntax is
  6160. used to construct them; this could be removed from the language and
  6161. you would have no empty or singleton tuples (like in ABC).
  6162.  
  6163. <A> You can also create empty and singleton tuples with slicing
  6164. operations.
  6165.  
  6166. <Q> But why are slicing operations needed for tuples?  ABC doesn't
  6167. have them.
  6168.  
  6169. Well, *sometimes* it is useful to treat tuples as sequences of
  6170. elements, and use subscripting or slice operations on them.  E.g.,
  6171. posix.stat returns an 11-tuple.  Some applications save the first
  6172. three elements of such a tuple (mode, inode, device) as a "unique
  6173. identifier" for a file.  Slicing (e.g., s[0:3]) is a convenient way of
  6174. extracting this.  But using a slice operation it is easy to construct
  6175. a singleton tuple (e.g., s[0:1]).
  6176.  
  6177. <Q> Then why can't tuple slices that produce singleton tuples be
  6178. forbidden or made to return the element instead, so that s[0:1] is the
  6179. same as s[0]?
  6180.  
  6181. There are many algorithms that can operate on all types of sequences
  6182. (tuples, lists and strings) using only subscripting, slicing and
  6183. concatenation, and in general these may construct singleton or empty
  6184. sequences, if only as intermediate results.  E.g., here's a function
  6185. to compute a list of all permutations of a sequence:
  6186.  
  6187.     def perm(l):
  6188.         if len(l) <= 1: return [l]
  6189.         r = []
  6190.         for i in range(len(l)):
  6191.             p = perm(l[:i] + l[i+1:])
  6192.             for x in p: r.append(l[i:i+1] + x)
  6193.         return r
  6194.  
  6195. For example, perm('abc') is ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
  6196. and perm((1, 2)) is [(1, 2), (2, 1)].  The latter constructs the
  6197. singletons (1,) and (2,) internally several times.
  6198.  
  6199. <Q> But couldn't tuple concatenation be redefined so that if either
  6200. argument were a single value, it would be promoted to a tuple?  Then
  6201. tuple slices of one element could return that element, avoiding a
  6202. sinleton tuple.
  6203.  
  6204. <A> Then perm((1, 2)) above would yield [3, 3]!  Because the final
  6205. results are constructing by concatenating the singletons (1,) and
  6206. (2,), which would have been replaced by the numbers 1 and 2.
  6207.  
  6208. <Q> But what if the concatenation operator were a different symbol
  6209. than integer addition, e.g., '++'?
  6210.  
  6211. <A> The perm() function would then require the dubious rule that that
  6212. 1++2 is defined as (1, 2).  Now compare this to 'a'++'b' -- this
  6213. obviously means 'ab' (since strings can also be concatenated) but then
  6214. the type of a++b would be hard to predict by the reader -- will it be
  6215. of the same type as a and b, or will it be the type of (a, b)?  I'm
  6216. sure this would cause lots of surprises.
  6217.  
  6218. <Q> What about doing it the other way around then?  I.e., singleton
  6219. tuples are not automatically degraded to their only element when
  6220. created, but when a singleton tuple is the argument of an operation
  6221. that requires a plain value, the first and only element of the tuple
  6222. is used instead.
  6223.  
  6224. <A> Well, the singleton may itself contain a singleton, e.g., ((1,),).
  6225. If we degenerate this to its first element we've still got a
  6226. singleton, (1,).
  6227.  
  6228. <Q> OK, suppose we apply the rule recursively?
  6229.  
  6230. <A> It will only confuse for users who wonder whether singletons
  6231. actually exist or not.  They conveniently vanish in so many places
  6232. that when first learning the language you believe they do not actually
  6233. exist.  In other words, the user's model of what's happening will
  6234. likely be one of the models rejected earlier, where singletons are
  6235. discarded as soon as they are created.  By the time they encounter a
  6236. counter-example it will be too late, and they may have written loads
  6237. of broken code, like this function to extract the middle element from
  6238. a tuple which returns a singleton tuple instead of an element from the
  6239. tuple:
  6240.  
  6241.     def middle(t):
  6242.         i = len(t) / 2
  6243.         return t[i:i+1]
  6244.  
  6245. The code is broken, but will work for simple examples:
  6246.  
  6247.     >>> a = 1, 10, 100, 1000, 10000
  6248.     >>> print middle(a) / 2
  6249.     50
  6250.  
  6251. Only when applied to a tuple containing lists the bug will show up:
  6252.  
  6253.     >>> p = []
  6254.     >>> q = [1, 10, 100]
  6255.     >>> r = range(10)
  6256.     >>> b = p, q, r
  6257.     >>> for x in middle(b): print x
  6258.     [1, 10, 100]
  6259.  
  6260. Expected was the same output as from:
  6261.  
  6262.     >>> for x in q: print x
  6263.     1
  6264.     10
  6265.     100
  6266.  
  6267. <Q> OK, singletons are useful, but why does there have to be this ugly
  6268. syntax like (1,) to create them?  Can't you just scrap that and make
  6269. slicing the *only* way to create singletons?
  6270.  
  6271. <A> Well, tuples have a representation on output, and as long as the
  6272. values it contains are simple enough, you can read a tuple as written
  6273. back in (with input() or eval()) and get an object with the same
  6274. value.  Since singleton tuples exist, they must have a representation
  6275. on output, and it is only fair that their representation can also be
  6276. read back in.  Also the singleton notation makes it easy to play with
  6277. singletons to find out how they work...
  6278.  
  6279. <Q> OK, I give up on tuples, they are perfect :-)  Since you mention
  6280. the way tuples are written on output, why are they *always* surrounded
  6281. by parentheses?  I thought you said that it's not the parentheses but
  6282. the comma that makes the tuple...
  6283.  
  6284. <A> This makes it easier to write tuples containing tuples.  If the
  6285. string representation of all object types is syntactically an atom,
  6286. the function for writing a tuple (or converting it to a string, which
  6287. uses the same rules) needn't know whether to surround the elements it
  6288. writes with parentheses or not.  (ABC uses the latter strategy, which
  6289. leads to more code.)
  6290.  
  6291. <Q> Oh, and by the way, why isn't print a function?
  6292.  
  6293. <A> Because it's such a heavily-used function that it deserves special
  6294. treatment -- now that it is a statement, you don't need to type
  6295. parentheses around the argument list.  Also, a statement it can use
  6296. special syntax to distinguish whether a newline should be appended or
  6297. not; if it were a function there would either have to be two functions
  6298. (like Pascal's write/writeln) or a special argument value (like "echo
  6299. -n" in the Bourne shell).
  6300.  
  6301. <Q> Let's shift attention to the function call syntax.  Why can't the
  6302. parentheses be optional, like in ABC, so I can write sin x instead of
  6303. sin(x)?
  6304.  
  6305. <A> And how do I call a function without parameters then?  Does x=f
  6306. have to call f if it happens to be a parameterless function, as is the
  6307. case in ABC [which has no function pointers] and also in Algol-68
  6308. [which does have function pointers, so it calls f except when x is of
  6309. type pointer to function]?
  6310.  
  6311. <Q> No, assume there are no parameterless functions, but you can
  6312. define a function of one argument that is discarded; you can call it
  6313. as either f None or f().
  6314.  
  6315. <A> Fair enough, although it's not particularly elegant -- I suppose
  6316. if I call it as f(1) the argument also gets discarded?  Anyway, what
  6317. do you do about the following ambiguity: a[1].  Does this call the
  6318. function a with list argument [1], or does it take element 1 of
  6319. sequence (list, tuple or string) a?  Surely requiring parentheses
  6320. there would only be more confusing:
  6321.  
  6322.     >>> def f x: return len x
  6323.     >>> a = [1]
  6324.     >>> f a
  6325.     1
  6326.     >>> f [1]
  6327.     *** TypeError: subscripting unsubscriptable object
  6328.  
  6329. <Q> Can't you resolve this ambiguity at run time, like you already do
  6330. for the '+' operator or for the call operator x() (which creates a
  6331. class instance if x happens to be a class object)?
  6332.  
  6333. <A> I think that would be ugly.  Also I cannot think of a situation
  6334. where the user would ever use the ambiguity in a polymorphic function,
  6335. unlike for the other two:
  6336.  
  6337.     # A function taking either strings or numbers
  6338.     def f(a, b):
  6339.         if a > b: return a
  6340.         else: return a+b
  6341.  
  6342.     # A function taking either a class or some other function that
  6343.     # creates an instance
  6344.     def g(creator):
  6345.         for i in range(10):
  6346.             instance = creator()
  6347.             if instance.acceptable(): return instance
  6348.         return None
  6349.  
  6350. <Q> What other uses are there for None, besides as return value from a
  6351. procedure?
  6352.  
  6353. <A> Almost all the same uses that a NULL pointer has in C.  An
  6354. important case it the use of None as an error return value (if for
  6355. some reason the error doesn' warrant raising an exception), e.g., this
  6356. function:
  6357.  
  6358.     def openprofile():
  6359.         for name in ('.profile', '/etc/Profile', '/usr/etc/Profile'):
  6360.             try:
  6361.                 return open(name, 'r')
  6362.             except IOError:
  6363.                 pass
  6364.         return None # No profile -- use default settings
  6365.  
  6366. which can be called like this:
  6367.  
  6368.     f = openprofile()
  6369.     if f:
  6370.         <read the profile>
  6371.         f.close()
  6372.  
  6373. Another use is for a class that may want to postpone creation and
  6374. initialization of a subcomponent to the first time it is needed.
  6375. E.g.:
  6376.  
  6377.     class C:
  6378.         def init(self):
  6379.             self.sub = None
  6380.             return self
  6381.         def usesub(self):
  6382.             if self.sub is None:
  6383.                 self.sub = makesub()
  6384.             <use self.sub>
  6385.  
  6386. None is also useful if a value is required but you aren't interested
  6387. in it, e.g., when using the keys of a dictionary to implement a set of
  6388. strings:
  6389.  
  6390.     class Set:
  6391.         def init(self):
  6392.             self.dict = {}
  6393.             return self
  6394.         def add(self, x):
  6395.             self.dict[x] = None
  6396.         def remove(self, x):
  6397.             if self.dict.has_key(x):
  6398.                 del self.dict[x]
  6399.         def ismember(self, x):
  6400.             return self.dict.has_key(x)
  6401.         # etc.
  6402.  
  6403.  
  6404. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6405. "What a senseless waste of human life"
  6406. 
  6407. 
  6408. To: python-list@cwi.nl
  6409. Subject: a neat/ugly hack
  6410. From: Guido van Rossum <guido@cwi.nl>
  6411. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6412. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6413. Date: Thu, 16 Jan 92 01:07:00 +0100
  6414. Sender: guido
  6415.  
  6416. After reading an article by adam@visix.com in comp.lang.misc, the
  6417. following occurred to me.  Is this net or ugly?  I kinda like it...
  6418.  
  6419. It will probably only make sense to you if you've read the file
  6420. misc/CLASSES in the Python distribution; it depends on details of
  6421. Python's class implementation (which are simple enough, just different
  6422. from other OO languages).  It also requires that class objects are
  6423. writable (which they are since 0.9.3).
  6424.  
  6425. Suppose we have a class whose objects have some kind of state which
  6426. modifies the meaning of one or more methods.  Traditionally this is
  6427. done by changing a flag which is tested by the affected methods.
  6428.  
  6429. Perhaps more efficiency or elegance can be gained by swapping method
  6430. pointers.  A naive implementation of this would be the following:
  6431.  
  6432.     class C:
  6433.         def init(self): ...; return self
  6434.         def var1(self): ...
  6435.         def var2(self): ...
  6436.         def setflag(self): self.var = self.var1
  6437.         def clrflag(self): self.var = self.var2
  6438.         def usevar(self): ...; self.var(); ...
  6439.  
  6440. The problem with this is that it creates circular references, which in
  6441. the current version of Python causes uncollectable garbage if the
  6442. object becomes unreachable.  (The data attribute self.var is either
  6443. self.var1 or self.var2; these aren't plain functions but *method*
  6444. objects which, amongst others, contain a pointer (accessible as
  6445. im_self) to the object.  If this is still unclear, write me and I'll
  6446. explain it in more detail.)
  6447.  
  6448. Now the fact that class declarations are executable becomes useful.
  6449. We use a function that is supposed to return a new "C" object.  This
  6450. function derives a new empty class "C1" from C and initializes it as
  6451. if it was a C.  The object can then assign a function to an attribute
  6452. of its __class__ and thereby change its methods:
  6453.  
  6454.     class C:
  6455.         def init(self): ...; return self
  6456.         def var1(self): ...
  6457.         def var2(self): ...
  6458.         def setflag(self): self.__class__.var = C.var1
  6459.         def clrflag(self): self.__class__.var = C.var2
  6460.         def usevar(self): ...; self.var(); ...
  6461.  
  6462.     def newC():
  6463.         class C1(C): pass
  6464.         return C1().init()
  6465.  
  6466. Each call to newC() creates a new class object C1.  This is essential;
  6467. if we were to move the definition of C1 out of newC(), calling
  6468. setflag() for one C object would change the meaning of var to var1 for
  6469. *all* C objects!
  6470.  
  6471. There are no circular references in this version: C.var1 and C.var2
  6472. are not method objects, they are functions that only get turned into
  6473. methods when called as self.var (or self.var1).
  6474.  
  6475. A disadvantage of this approach is that it won't work for derived
  6476. classes of C.  For some cases this can be solved by passing the class
  6477. to be instantiated as a parameter to newC() (which is better renamed to
  6478. new() then).  It gets trickier when some derived class also uses this
  6479. hack; then that class must take care of the needs of its base class(es).
  6480. (This is probably trivial if the hack is used exactly as shown here,
  6481. since all base classes can place their switching methods in the same
  6482. "C1" class.)
  6483.  
  6484. Another disadvantage is that all other method calls become epsilon
  6485. slower, since the chain of base classes is one longer.  I haven't
  6486. measured this, it should cost no more than one dictionary lookup in a
  6487. tiny dictionary.
  6488.  
  6489. A variant would require that an instance's __class__ attribute be
  6490. writeable (this is currently not the case but it would be a one-line
  6491. change to the interpreter); here we would just have two classes C1 and
  6492. C2 derived from C and let the setflag() methods switch the instance's
  6493. __class__ attribute between C1 and C2.  In fact the mere possibility
  6494. of this makes me doubt the read-only-ness of __class__ (or __bases__,
  6495. for that matter).
  6496.  
  6497. (I wonder if this hack allows one to write Python programs in the
  6498. "delegation" paradigm rather than the "inheritance" paradigm.  Or am I
  6499. completely off base here?)
  6500.  
  6501. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6502. "If this is Bolton, I shall return to the pet shop"
  6503. 
  6504. 
  6505. Received: by charon.cwi.nl with SMTP; Thu, 16 Jan 1992 01:07:18 +0100
  6506. Received: by voorn.cwi.nl with SMTP; Thu, 16 Jan 1992 00:07:01 GMT
  6507. Message-Id: <9201160007.AA07722@voorn.cwi.nl>
  6508. To: python-list@cwi.nl
  6509. Subject: a neat/ugly hack
  6510. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  6511. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6512. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6513. Date: Thu, 16 Jan 92 01:07:00 +0100
  6514. Sender: Guido.van.Rossum@cwi.nl
  6515.  
  6516. After reading an article by adam@visix.com in comp.lang.misc, the
  6517. following occurred to me.  Is this net or ugly?  I kinda like it...
  6518.  
  6519. It will probably only make sense to you if you've read the file
  6520. misc/CLASSES in the Python distribution; it depends on details of
  6521. Python's class implementation (which are simple enough, just different
  6522. from other OO languages).  It also requires that class objects are
  6523. writable (which they are since 0.9.3).
  6524.  
  6525. Suppose we have a class whose objects have some kind of state which
  6526. modifies the meaning of one or more methods.  Traditionally this is
  6527. done by changing a flag which is tested by the affected methods.
  6528.  
  6529. Perhaps more efficiency or elegance can be gained by swapping method
  6530. pointers.  A naive implementation of this would be the following:
  6531.  
  6532.     class C:
  6533.         def init(self): ...; return self
  6534.         def var1(self): ...
  6535.         def var2(self): ...
  6536.         def setflag(self): self.var = self.var1
  6537.         def clrflag(self): self.var = self.var2
  6538.         def usevar(self): ...; self.var(); ...
  6539.  
  6540. The problem with this is that it creates circular references, which in
  6541. the current version of Python causes uncollectable garbage if the
  6542. object becomes unreachable.  (The data attribute self.var is either
  6543. self.var1 or self.var2; these aren't plain functions but *method*
  6544. objects which, amongst others, contain a pointer (accessible as
  6545. im_self) to the object.  If this is still unclear, write me and I'll
  6546. explain it in more detail.)
  6547.  
  6548. Now the fact that class declarations are executable becomes useful.
  6549. We use a function that is supposed to return a new "C" object.  This
  6550. function derives a new empty class "C1" from C and initializes it as
  6551. if it was a C.  The object can then assign a function to an attribute
  6552. of its __class__ and thereby change its methods:
  6553.  
  6554.     class C:
  6555.         def init(self): ...; return self
  6556.         def var1(self): ...
  6557.         def var2(self): ...
  6558.         def setflag(self): self.__class__.var = C.var1
  6559.         def clrflag(self): self.__class__.var = C.var2
  6560.         def usevar(self): ...; self.var(); ...
  6561.  
  6562.     def newC():
  6563.         class C1(C): pass
  6564.         return C1().init()
  6565.  
  6566. Each call to newC() creates a new class object C1.  This is essential;
  6567. if we were to move the definition of C1 out of newC(), calling
  6568. setflag() for one C object would change the meaning of var to var1 for
  6569. *all* C objects!
  6570.  
  6571. There are no circular references in this version: C.var1 and C.var2
  6572. are not method objects, they are functions that only get turned into
  6573. methods when called as self.var (or self.var1).
  6574.  
  6575. A disadvantage of this approach is that it won't work for derived
  6576. classes of C.  For some cases this can be solved by passing the class
  6577. to be instantiated as a parameter to newC() (which is better renamed to
  6578. new() then).  It gets trickier when some derived class also uses this
  6579. hack; then that class must take care of the needs of its base class(es).
  6580. (This is probably trivial if the hack is used exactly as shown here,
  6581. since all base classes can place their switching methods in the same
  6582. "C1" class.)
  6583.  
  6584. Another disadvantage is that all other method calls become epsilon
  6585. slower, since the chain of base classes is one longer.  I haven't
  6586. measured this, it should cost no more than one dictionary lookup in a
  6587. tiny dictionary.
  6588.  
  6589. A variant would require that an instance's __class__ attribute be
  6590. writeable (this is currently not the case but it would be a one-line
  6591. change to the interpreter); here we would just have two classes C1 and
  6592. C2 derived from C and let the setflag() methods switch the instance's
  6593. __class__ attribute between C1 and C2.  In fact the mere possibility
  6594. of this makes me doubt the read-only-ness of __class__ (or __bases__,
  6595. for that matter).
  6596.  
  6597. (I wonder if this hack allows one to write Python programs in the
  6598. "delegation" paradigm rather than the "inheritance" paradigm.  Or am I
  6599. completely off base here?)
  6600.  
  6601. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6602. "If this is Bolton, I shall return to the pet shop"
  6603. 
  6604. 
  6605. Received: by charon.cwi.nl with SMTP; Thu, 16 Jan 1992 14:33:04 +0100
  6606. Received: by voorn.cwi.nl with SMTP; Thu, 16 Jan 1992 13:33:03 GMT
  6607. Message-Id: <9201161333.AA09516@voorn.cwi.nl>
  6608. To: python-list@cwi.nl
  6609. Subject: Getting rid of '\E'
  6610. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  6611. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6612. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6613. Date: Thu, 16 Jan 92 14:33:01 +0100
  6614. Sender: Guido.van.Rossum@cwi.nl
  6615.  
  6616. Currently, Python defines '\E' in string literals as the ASCII ESC
  6617. character.  This is the only \-escape that is different from ANSI C.
  6618. I have never used it, and that it's actually easy enough to write '\033'.
  6619. Would anybody care if I took it out of the language?
  6620.  
  6621. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6622. "Repo Man's got all night, *every* night"
  6623. 
  6624. 
  6625. To: python-list@cwi.nl
  6626. Subject: Getting rid of '\E'
  6627. From: Guido van Rossum <guido@cwi.nl>
  6628. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6629. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6630. Date: Thu, 16 Jan 92 14:33:01 +0100
  6631. Sender: guido
  6632.  
  6633. Currently, Python defines '\E' in string literals as the ASCII ESC
  6634. character.  This is the only \-escape that is different from ANSI C.
  6635. I have never used it, and that it's actually easy enough to write '\033'.
  6636. Would anybody care if I took it out of the language?
  6637.  
  6638. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6639. "Repo Man's got all night, *every* night"
  6640. 
  6641. 
  6642. Received: by charon.cwi.nl with SMTP; Mon, 20 Jan 1992 13:54:22 +0100
  6643. Received: by voorn.cwi.nl with SMTP; Mon, 20 Jan 1992 12:54:21 GMT
  6644. Message-Id: <9201201254.AA20517@voorn.cwi.nl>
  6645. To: python-list@cwi.nl
  6646. Subject: should left shift test for overflow?
  6647. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  6648. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6649. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6650. Date: Mon, 20 Jan 92 13:54:19 +0100
  6651. Sender: Guido.van.Rossum@cwi.nl
  6652.  
  6653. Hi folks, after the successful (:-) event of dropping '\E', here I am
  6654. again with a microscopic nit in the definition of Python on which I'd
  6655. like to see your considered opinion.
  6656.  
  6657. I am currently in the process of rationalizing Python's shift and mask
  6658. operations.  Long (arbitrary length) integers will pretend they're
  6659. using 2's complement notation with an infinite number of bits (e.g., 1
  6660. is ...0001, -1 is ...1111, -2 is ...1110, and so on.  This will mean
  6661. that any expression involving plain integers that doesn't raise an
  6662. exception has the *same* result when one or more of the operands are
  6663. replaced by a long integer with the same value.
  6664.  
  6665. Currently the only operator for which this isn't the case is left shift.
  6666. Bits shifted out of plain (32-bit) integers are dropped mercilessly.
  6667. Of course this is done in analogy with C, but it loses the
  6668. corrspondence with long integers; e.g., 1000<<25 is -805306368 but
  6669. 1000L<<25 is 33554432000L.  It also loses the correspondence between
  6670. left-shift and multiplication by a power of 2.
  6671.  
  6672. But the advantage of dropping the bits is that I can write the
  6673. following function to read a 32-bit binary integer from a file:
  6674.  
  6675. def rdlongb(f): # Read a "long" (32-bit int) in big-endian format
  6676.     s = f.read(4)
  6677.     a, b, c, d = ord(s[0]), ord(s[1]), ord(s[2]), ord(s[3])
  6678.     return a<<24 | b<<16 | c<<8 | d
  6679.  
  6680. If we interpret left-shift as multiplication by a power of 2, the
  6681. "a<<24" part would cause overflow if the sign bit of the byte was on
  6682. (i.e., if a >= 128).  To fix this, I would have to add something like
  6683. "if a >= 128: a = a-256" before the return statement.
  6684.  
  6685. Note that the same problem does not exist for right shifts: dropping
  6686. bits there is perfectly analogous with division by a power of two
  6687. (e.g., 1000>>25 is 0 in any domain).  Also note that I propose to
  6688. change integer division of negative numbers such that -1/2 is -1: x/y
  6689. is floor(x//y) if x//y means mathematical (loss-less) division, so the
  6690. correspondence between (sign-extending) right-shift and division is
  6691. perfect.  Modulo will be fixed accordingly: -1%2 will be 1.
  6692.  
  6693. My question is, what's more important here: the ability to construct a
  6694. negative number by left-shifting a positive number, or correspondence
  6695. with long integers and multiplication?
  6696.  
  6697. (If you're still with me, you may also tell me if you can find a
  6698. reason for unsigned shifts.  I currently plan to make all shift
  6699. operations sign-preserving, for correspondence with
  6700. multiplication/division with powers of 2.  What are the advantages of
  6701. unsigned shifts?)
  6702.  
  6703. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6704. "Programming - the skill (not an art nor a science) of intentional
  6705. premature binding.  The best programmers are those who can best make
  6706. proper decisions too early." --B. A. Creech, 1969
  6707. 
  6708. 
  6709. To: python-list
  6710. Subject: should left shift test for overflow?
  6711. From: Guido van Rossum <guido@cwi.nl>
  6712. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  6713. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  6714. Date: Mon, 20 Jan 92 13:54:19 +0100
  6715. Sender: guido
  6716.  
  6717. Hi folks, after the successful (:-) event of dropping '\E', here I am
  6718. again with a microscopic nit in the definition of Python on which I'd
  6719. like to see your considered opinion.
  6720.  
  6721. I am currently in the process of rationalizing Python's shift and mask
  6722. operations.  Long (arbitrary length) integers will pretend they're
  6723. using 2's complement notation with an infinite number of bits (e.g., 1
  6724. is ...0001, -1 is ...1111, -2 is ...1110, and so on.  This will mean
  6725. that any expression involving plain integers that doesn't raise an
  6726. exception has the *same* result when one or more of the operands are
  6727. replaced by a long integer with the same value.
  6728.  
  6729. Currently the only operator for which this isn't the case is left shift.
  6730. Bits shifted out of plain (32-bit) integers are dropped mercilessly.
  6731. Of course this is done in analogy with C, but it loses the
  6732. corrspondence with long integers; e.g., 1000<<25 is -805306368 but
  6733. 1000L<<25 is 33554432000L.  It also loses the correspondence between
  6734. left-shift and multiplication by a power of 2.
  6735.  
  6736. But the advantage of dropping the bits is that I can write the
  6737. following function to read a 32-bit binary integer from a file:
  6738.  
  6739. def rdlongb(f): # Read a "long" (32-bit int) in big-endian format
  6740.     s = f.read(4)
  6741.     a, b, c, d = ord(s[0]), ord(s[1]), ord(s[2]), ord(s[3])
  6742.     return a<<24 | b<<16 | c<<8 | d
  6743.  
  6744. If we interpret left-shift as multiplication by a power of 2, the
  6745. "a<<24" part would cause overflow if the sign bit of the byte was on
  6746. (i.e., if a >= 128).  To fix this, I would have to add something like
  6747. "if a >= 128: a = a-256" before the return statement.
  6748.  
  6749. Note that the same problem does not exist for right shifts: dropping
  6750. bits there is perfectly analogous with division by a power of two
  6751. (e.g., 1000>>25 is 0 in any domain).  Also note that I propose to
  6752. change integer division of negative numbers such that -1/2 is -1: x/y
  6753. is floor(x//y) if x//y means mathematical (loss-less) division, so the
  6754. correspondence between (sign-extending) right-shift and division is
  6755. perfect.  Modulo will be fixed accordingly: -1%2 will be 1.
  6756.  
  6757. My question is, what's more important here: the ability to construct a
  6758. negative number by left-shifting a positive number, or correspondence
  6759. with long integers and multiplication?
  6760.  
  6761. (If you're still with me, you may also tell me if you can find a
  6762. reason for unsigned shifts.  I currently plan to make all shift
  6763. operations sign-preserving, for correspondence with
  6764. multiplication/division with powers of 2.  What are the advantages of
  6765. unsigned shifts?)
  6766.  
  6767. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  6768. "Programming - the skill (not an art nor a science) of intentional
  6769. premature binding.  The best programmers are those who can best make
  6770. proper decisions too early." --B. A. Creech, 1969
  6771. 
  6772. 
  6773. Received: by charon.cwi.nl with SMTP; Tue, 21 Jan 1992 22:26:21 +0100
  6774. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa10317;
  6775.           21 Jan 92 16:26 EST
  6776. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  6777.     id AA07838; Tue, 21 Jan 92 16:17:19 EST
  6778. Date: Tue, 21 Jan 92 16:17:19 EST
  6779. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  6780. Message-Id: <9201212117.AA07838@aemsun.med.Virginia.EDU>
  6781. To: python-list@cwi.nl
  6782. Subject: Directory usage tree's in perl & python.
  6783.  
  6784.  
  6785. There has been a series of good-perl/bad-perl exchanges in comp.lang.perl,
  6786. that produced (among other things) this exchange:
  6787.  
  6788. From: flee@cs.psu.edu (Felix Lee) :
  6789.  
  6790. And Perl is definitely awkward with data types.  I haven't yet found a
  6791. pleasant way of shoving non-trivial data types into Perl's grammar.
  6792. Here's a very simple problem that's tricky to express in Perl: process
  6793. the output of "du" to produce output that's indented to reflect the
  6794. tree structure, and with each subtree sorted by size.  Something like:
  6795.     434 /etc
  6796.       |     344 .
  6797.       |      50 install
  6798.       |      35 uucp
  6799.       |       3 nserve
  6800.       |       |       2 .
  6801.       |       |       1 auth.info
  6802.       |       1 sm
  6803.       |       1 sm.bak
  6804.  
  6805. ---
  6806.  
  6807.  
  6808. And from:  Tom Christiansen           tchrist@convex.com      convex!tchrist:
  6809.  
  6810. :Here's a very simple problem that's tricky to express in Perl: process
  6811. :the output of "du" to produce output that's indented to reflect the
  6812. :tree structure, and with each subtree sorted by size.  Something like:
  6813. :    434 /etc
  6814. :      |     344 .
  6815. :      |      50 install
  6816. :      |      35 uucp
  6817. :      |       3 nserve
  6818. :      |       |       2 .
  6819. :      |       |       1 auth.info
  6820. :      |       1 sm
  6821. :      |       1 sm.bak
  6822.  
  6823. At first I thought I could just keep one local list around
  6824. at once, but this seems inherently recursive.  Which means 
  6825. I need an real recursive data structure.  Maybe you could
  6826. do it with one of the %assoc arrays Larry uses in the begat
  6827. programs, but I broke down and got dirty.  I think the hardest
  6828. part was matching Felix's desired output exactly.  It's not 
  6829. blazingly fast: I should probably inline the &childof routine,
  6830. but it *was* faster to write than I could have written the 
  6831. equivalent C program.
  6832.  
  6833.  
  6834. --tom
  6835.  
  6836.  
  6837. #!/usr/bin/perl
  6838. # dutree -- tchrist@convex.com
  6839.  
  6840. @lines = `du @ARGV`;
  6841. chop(@lines);
  6842. &input($top = pop @lines);
  6843. &output($top);
  6844. exit $?;
  6845.  
  6846. sub input {
  6847.     local($root, *kid, $him) = @_[0,0];
  6848.     while (@lines && &childof($root, $lines[$#lines])) {
  6849.     &input($him = pop(@lines));
  6850.     push(@kid, $him);
  6851.     } 
  6852.     @kid = &sizesort(*kid);
  6853.  
  6854. sub output {
  6855.     local($root, *kid, $prefix) = @_[0,0,1];
  6856.     local($size, $path) = split(' ', $root);
  6857.     $path =~ s!.*/!!;
  6858.     $line = sprintf("%${width}d %s", $size, $path);
  6859.     print $prefix, $line, "\n";
  6860.     $prefix .= $line;
  6861.     $prefix =~ s/\d /| /; $prefix =~ s/[^|]/ /g;
  6862.     local($width) = $kid[0] =~ /(\d+)/ && length("$1");
  6863.     for (@kid) { &output($_, $prefix); };
  6864.  
  6865. sub sizesort {
  6866.     local(*list, @index) = shift;
  6867.     sub bynum { $index[$b] <=> $index[$a]; }
  6868.     for (@list) { push(@index, /(\d+)/); } 
  6869.     @list[sort bynum 0..$#list];
  6870.  
  6871. sub childof {
  6872.     local(@pair) = @_;
  6873.     for (@pair) { s/^\d+\s+//g; }
  6874.     index($pair[1], $pair[0]) >= 0;
  6875.  
  6876. --- 
  6877.  
  6878. The above prompted me to try the same problem in python. 
  6879. First attempt was a simple version, which leads me to discover
  6880. that it is an inherently recursively problem ( as Tom states above...
  6881. If I could read Perl better without poking into the manual, It might
  6882. have been more obvious - I didn't have to perl man with me, so I didn't
  6883. try very hard to read it. )
  6884.  
  6885. The simple version has 2 problems: 
  6886.   (1) After writing the python script, I re-read the thread, and saw
  6887.       that they had asked for nodes sorted by size, ( at least the terminals,
  6888.       I'm not sure if they meant the whole tree. )
  6889.       This was just an oversight, BUT I don't see a neat way to 
  6890.       patch it into this code. 
  6891.   (2)  "Dangling "|"'s" : This is what made me realize it needs a 
  6892.       recursize solution. 
  6893.  
  6894. So back to the drawing board for version 2 :-( 
  6895. Happy to hear any suggestions!
  6896.  
  6897.  
  6898. Simple (version 1) of dutree and output:
  6899.  
  6900. #!/usr/local/python
  6901. # dutree.py  -- Steven D. Majewski (sdm7g@Virginia.EDU)
  6902. # print a directory tree
  6903. #
  6904.  
  6905. import string,path,posix,sys
  6906.  
  6907. # du( root ) ==> [ sorted list of [ path, usage ] ]
  6908. def du( root ):
  6909.  d = posix.popen('du '+root ,'r' ).readlines()    # slurp up 'du' output
  6910.  L = []
  6911.  for x in d : 
  6912.    xx =string.split(x)        # split fields: [ usage, path ],
  6913.    xx.reverse()            # swap ( [path,usage] sorts better! )
  6914.    L.append( xx )        # append 
  6915.  L.sort()
  6916.  return L
  6917.  
  6918. # print L as a tree, where: L = [ sorted list of [ path, usage ] ]
  6919. def printdu( L ):
  6920.   print L[0]
  6921.   comlen = len(string.splitfields( L[0][0], '/' )) + 1
  6922.   for x in L[1:]:
  6923.      ea = string.splitfields( x[0], '/' ) + x[-1:]
  6924.      print (' '*6+'|')*(len(ea)-comlen)+'__',string.rjust(ea[-2],4)+':',ea[-1]
  6925.   print '\n' 
  6926.  
  6927. # tree is defined mainly for interactive use. 
  6928. # Usage:
  6929. # >>>from dutree import tree
  6930. # >>>tree( '/usr/local' )
  6931. # ( doesn't return anything - only prints tree.)
  6932.  
  6933. def tree( root ):
  6934.   printdu(du(root))
  6935.  
  6936.  
  6937. for d in sys.argv[1:] : tree( d )    # this is main() for dutree.py 
  6938.  
  6939.  
  6940.  
  6941. -- 
  6942. and sample output: 
  6943.  
  6944. ('/local/lang/Python', '9761')
  6945.       |__ demo: 542
  6946.       |      |__ scripts: 202
  6947.       |      |__  sgi: 176
  6948.       |      |      |__   al: 20
  6949.       |      |      |__ audio: 4
  6950.       |      |      |__ audio_stdwin: 17
  6951.       |      |      |__   gl: 69
  6952.       |      |      |      |__ glstdwin: 27
  6953.       |      |      |__ gl_panel: 64
  6954.       |      |      |      |__ apanel: 5
  6955.       |      |      |      |__ flying: 30
  6956.       |      |      |      |__ nurbs: 9
  6957.       |      |      |      |__ twoview: 17
  6958.       |      |__ sockets: 17
  6959.       |      |__ stdwin: 145
  6960.       |      |      |__ ibrowse: 81
  6961.       |__  doc: 970
  6962.       |__  lib: 382
  6963.       |__ local: 86
  6964.       |__ misc: 77
  6965.       |__ readline: 1107
  6966.       |      |__  doc: 256
  6967.       |      |__ examples: 14
  6968.       |__  src: 2733
  6969.       |__ stdwin: 3859
  6970.       |      |__ Appls: 227
  6971.       |      |      |__  dpv: 84
  6972.       |      |      |__ klok: 22
  6973.       |      |      |__ miniedit: 45
  6974.       |      |      |__ repeat: 9
  6975.       |      |      |__ test: 48
  6976.       |      |      |__ tetris: 18
  6977.       |      |__ Build: 2858
  6978.       |      |      |__ sun4: 2857
  6979.       |      |      |      |__  x11: 2856
  6980.       |      |      |      |      |__ klok: 291
  6981.       |      |      |      |      |__  lib: 1647
  6982.       |      |      |      |      |__ miniedit: 373
  6983.       |      |      |      |      |__ repeat: 291
  6984.       |      |      |      |      |__ tetris: 253
  6985.       |      |__ Conf: 46
  6986.       |      |__  Doc: 146
  6987.       |      |      |__  man: 38
  6988.       |      |__  Gen: 13
  6989.       |      |__    H: 42
  6990.       |      |__ Packs: 112
  6991.       |      |      |__ textedit: 49
  6992.       |      |      |__   vt: 62
  6993.       |      |__ Ports: 401
  6994.       |      |      |__ alfa: 77
  6995.       |      |      |__  mac: 98
  6996.       |      |      |__ msdos: 33
  6997.       |      |      |__ vtrm: 48
  6998.       |      |      |__  x11: 144
  6999.       |      |__ Tools: 4
  7000.  
  7001. ---
  7002. Note: ^ the dangling "|" ! :-( 
  7003.  -- Steve. 
  7004.  
  7005.  
  7006. 
  7007. 
  7008. Received: by charon.cwi.nl with SMTP; Wed, 22 Jan 1992 08:02:15 +0100
  7009. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  7010.     (5.61/UUNET-internet-primary) id AA00770; Wed, 22 Jan 92 02:02:12 -0500
  7011. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  7012.     (queueing-rmail) id 020131.29377; Wed, 22 Jan 1992 02:01:31 EST
  7013. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  7014.     id AA22084; Wed, 22 Jan 92 01:37:15 EST
  7015. Received: by kaos.ksr.com (4.0/SMI-3.2)
  7016.     id AA10866; Wed, 22 Jan 92 01:37:14 EST
  7017. Message-Id: <9201220637.AA10866@kaos.ksr.com>
  7018. To: sdm7g@Virginia.EDU
  7019. Subject: Re: Directory usage tree's in perl & python.
  7020. Cc: python-list@cwi.nl
  7021. Date: Wed, 22 Jan 92 01:37:13 EST
  7022. From: Tim Peters <tim@ksr.com>
  7023.  
  7024. >  [steve m]
  7025. >  There has been a series of good-perl/bad-perl exchanges ...
  7026. >  ...
  7027. >  If I could read Perl better without poking into the manual, It might
  7028. >  have been more obvious - I didn't have the perl man with me, ...
  7029.  
  7030. Wouldn't have helped <grin> -- seriously, the key to the Perl version is
  7031. the "*kid" construction, which is playing games with Perl's internal
  7032. symbol tables as a way to fake a "real" data structure.  It's very
  7033. tricky stuff, & the Perl book rushes over it, giving only a few trivial
  7034. examples after a hand-waving explanation.  Perl wizards all know how to
  7035. use it, though -- they don't have much choice <grin/frown>.
  7036.  
  7037. > ... they had asked for nodes sorted by size, ( at least the terminals,
  7038. > I'm not sure if they meant the whole tree. )
  7039.  
  7040. Pretty sure Felix Lee meant the whole tree, at each level.
  7041.  
  7042. > ... "Dangling "|"'s" : This is what made me realize it needs a
  7043. > recursive solution.
  7044.  
  7045. The Perl version also has "dangling |'s", & I *like* them!  Agree
  7046. they're hard to get rid of if you really want to; but think Lee had the
  7047. "sorted at each level" property in mind when he claimed a natural
  7048. advantage for recursive data structures (you need to sort at an
  7049. arbitrary number of different levels, without mixing things up across
  7050. levels -- hard to do with a flat data structure!).
  7051.  
  7052. FYI, I'll attach a different Python stab below.  Notes:
  7053.  
  7054.  - This tries to look more like the Perl version in the appearance of
  7055.    the output.
  7056.  
  7057.  - The Perl version was written to accept an arbitary number of
  7058.    directory arguments (@lines = `du @ARGV`), but it actually (in
  7059.    effect) ignores all but one of them; the Python version pays
  7060.    attention to all of them.
  7061.  
  7062.  - When invoked via, e.g., "dutree /dir1/dir2/dir3", the Perl version
  7063.    drops the "/dir1/dir2/" portion on the floor by the time it prints
  7064.    stuff; the Python version retains it.
  7065.  
  7066.  - The Perl version is at least 3x faster than the Python version.  This
  7067.    is more my doing than Python's:  I didn't try to take advantage of
  7068.    du's output being a postorder traversal, or play any clever tricks
  7069.    based on looking for longest matching string prefixes etc.  The sole
  7070.    goal I had in writing a Python version was to minimize my own
  7071.    programming time, so it's straightforward and the pieces are (in some
  7072.    well-hidden <grin> sense) more general than they need to be.  Despite
  7073.    that, the Python version turned out to be the same number of lines as
  7074.    the Perl version.
  7075.  
  7076.  - This is certainly easier to program in Python than in Perl!  The only
  7077.    place that felt a little strained was in the sorting, where the
  7078.    "size" field had to be the first component so that Python's
  7079.    list.sort method would use the size as the primary sort key.  Perl
  7080.    does have an advantage here in that its "sort" verb allows specifying
  7081.    an arbitrary Perl routine as the comparison predicate; but one could
  7082.    code up a similar verb in Python.
  7083.  
  7084.    When multiple sub-directories have the same size, my Python version
  7085.    lists them in reverse alphabetical order; the Perl version
  7086.    effectively lists them in a random order.  Possible to fix either
  7087.    version to be nicer about this, but I think it's a bit more strained
  7088.    to fix in Python via fooling the built-in "sort".
  7089.  
  7090.  - I'm afraid my Python version may be as hard to understand as the Perl
  7091.    version.  But hope that's just a side-effect of using complex data
  7092.    structures without explaining them <grin>.
  7093.  
  7094.    Briefly, the 'suck_up_du' routine constructs a dictionary mapping
  7095.    path components into [size, dictionary] pairs, where the contained
  7096.    dictionary is again of the same type (mapping the next path
  7097.    components into [size, dictionary] pairs, and so on & so on).  This
  7098.    makes it very easy to capture the parent-child relationships without
  7099.    needing string analysis any fancier than splitting on '/'.
  7100.  
  7101.    But Python doesn't have a way to sort dictionaries directly, so the
  7102.    "dict2list" function converts the recursive dictionary structure into
  7103.    a list of [size, path_component_name, list_of_children] triples,
  7104.    where the list_of_children is again of the same (list-of-triples)
  7105.    type.  The built-in list.sort() method automatically reorders the
  7106.    triples by the "size" field, and a list.reverse() then gets them in
  7107.    descending order.  There's also some trickiness to deal with "empty"
  7108.    path components arising from my being too lazy earlier to factor out
  7109.    longest matching directory prefixes.
  7110.  
  7111.    The output routine 'dump' is then a trivial recursive walk over the
  7112.    recursive list-of-triples structure.
  7113.  
  7114. Example output:
  7115.  
  7116.   8161 /lang/work/build/kap/kap12/Python
  7117.      |  1605 src
  7118.      |   839 readline
  7119.      |     |   256 doc
  7120.      |     |    14 examples
  7121.      |   532 demo
  7122.      |     |   206 scripts
  7123.      |     |   176 sgi
  7124.      |     |     |    69 gl
  7125.      |     |     |     |    27 glstdwin
  7126.      |     |     |    64 gl_panel
  7127.      |     |     |     |    30 flying
  7128.      |     |     |     |    17 twoview
  7129.      |     |     |     |     9 nurbs
  7130.      |     |     |     |     5 apanel
  7131.      |     |     |    20 al
  7132.      |     |     |    17 audio_stdwin
  7133.      |     |     |     4 audio
  7134.      |     |   131 stdwin
  7135.      |     |     |    67 ibrowse
  7136.      |     |    17 sockets
  7137.      |   284 lib
  7138.      |   189 doc
  7139.      |    76 misc
  7140.      |    31 local
  7141.  
  7142. I strongly encourage anyone comparing Python with Perl to take a whack
  7143. at this exercise -- very educational!  Hats off to Mr Lee for a fine
  7144. example ...
  7145.  
  7146. hoping-to-see-a-better-python-solution-too!-ly y'rs  - tim
  7147.  
  7148. Tim Peters   Kendall Square Research Corp
  7149. tim@ksr.com,         ksr!tim@uunet.uu.net
  7150.  
  7151. #!/usr/local/bin/python
  7152.  
  7153. import string, posix, sys
  7154.  
  7155. def suck_up_du( dirlist ):
  7156.     answer = {}
  7157.         for line in posix.popen( 'du ' + dirlist, 'r' ).readlines():
  7158.             [size, path] = string.split( line[:-1] ) # chop newline
  7159.             size = eval(size)    # convert to int
  7160.             insert( answer, path, size )
  7161.         return answer
  7162.  
  7163. def insert( dict, path, size ):
  7164.     components = string.splitfields( path, '/' )
  7165.     for chunk in components[:-1]:
  7166.         if not dict.has_key( chunk ): dict[chunk] = [-1, {}]
  7167.         dict = dict[chunk][1]
  7168.     chunk = components[-1]
  7169.     if not dict.has_key( chunk ): dict[chunk] = [-1, {}]
  7170.     dict[chunk][0] = size
  7171.  
  7172. def dict2list( nameprefix, dict ):
  7173.     answer = []
  7174.     for key in dict.keys():
  7175.         [size, kids] = dict[key]
  7176.         key = nameprefix + key
  7177.         if size < 0:
  7178.             for triple in dict2list( key + '/', kids ):
  7179.                 answer.append( triple )
  7180.         else:
  7181.             answer.append( [size, key, dict2list('',kids)] )
  7182.     answer.sort(); answer.reverse()
  7183.     return answer
  7184.  
  7185. def dump( prefix, list ):
  7186.     for [size,name,kids] in list:
  7187.         print prefix + string.rjust(`size`,6), name
  7188.         dump( prefix + ' '*5 + '|', kids )
  7189.  
  7190. def dudump( dirlist ):
  7191.     dump( '', dict2list( '', suck_up_du( dirlist ) ) )
  7192.  
  7193. dudump( string.join( sys.argv[1:] ) )
  7194.  
  7195. >>> END OF MSG
  7196. 
  7197. 
  7198. Replied: Thu, 23 Jan 92 11:36:33 +0100
  7199. Replied: "python-list@cwi.nl "
  7200. Received: by charon.cwi.nl with SMTP; Thu, 23 Jan 1992 00:23:15 +0100
  7201. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa03387;
  7202.           22 Jan 92 18:23 EST
  7203. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  7204.     id AA09307; Wed, 22 Jan 92 18:14:23 EST
  7205. Date: Wed, 22 Jan 92 18:14:23 EST
  7206. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  7207. Message-Id: <9201222314.AA09307@aemsun.med.Virginia.EDU>
  7208. To: python-list@cwi.nl
  7209. Subject: du tree 
  7210.  
  7211. Thanks, Tim. 
  7212.  
  7213. Guido also posted a solution to comp.lang.perl. ( In python, that is. )
  7214. [ Anyone want it re-posted to the mailing list, or is that redundant
  7215.   for this audience. ]
  7216.  
  7217. You might also want to post your version ( and your comparison of 
  7218. perl vs. python ). Then again, you might not. Some of the perl folks 
  7219. seem to have pretty thin skin when it comes to criticism. 
  7220.  
  7221. [ There were some references to perl critics as "ankle-biters" ! 
  7222.   Hey - even N.Wirth had to try his hand at designing a half-dozen
  7223.   or so languages ... and most of the complaints about Pascal were
  7224.   due to the fact that folks were trying to use it for things it 
  7225.   was never designed for in the first place! ... But I don't think
  7226.   Larry Wall himself is as thin skinned as some of his defenders. ]
  7227.  
  7228.  
  7229. Another thread for discussion is:
  7230.  What is missing from python that is in perl. 
  7231.  ( Or library - but major features only, i.e. something that is a
  7232.    real "application stopper" ! )
  7233.  
  7234. My nomination: 
  7235.   pack/unpack to/from binary structures ( for reading binary files )
  7236.   and the python equivalent of 'h2ph' to convert ".h" files into 
  7237.   a "record description". 
  7238.   ( I don't *think* there is a way to do this in python. Correct me
  7239.     if I'm wrong. )
  7240.  
  7241. Any suggestions for a syntax for that routine any different than
  7242. the perl equivalent ?  If we exclude longints, the major difference
  7243. I see is dealing with possibly recursive structures: lists, tuples
  7244. and dictionaries. Should the be disallowed ? ( Probably dictionaries
  7245. should be disallowed. ) Should they require a nested format specification ?
  7246. How about wildcard lengths for homogeneous sequences ? ( which might 
  7247. suggest a 'length of previous/next sequence in bytes' specifier ? ) 
  7248.  
  7249. - Steve
  7250. 
  7251. 
  7252. Received: by charon.cwi.nl with SMTP; Thu, 23 Jan 1992 11:36:34 +0100
  7253. Received: by voorn.cwi.nl with SMTP; Thu, 23 Jan 1992 10:36:33 GMT
  7254. Message-Id: <9201231036.AA03628@voorn.cwi.nl>
  7255. To: python-list@cwi.nl
  7256. Subject: Re: du tree 
  7257. In-Reply-To: Your message of "Wed, 22 Jan 92 18:14:23 MET."
  7258.              <9201222314.AA09307@aemsun.med.Virginia.EDU> 
  7259. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  7260. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  7261. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  7262. Date: Thu, 23 Jan 92 11:36:33 +0100
  7263. Sender: Guido.van.Rossum@cwi.nl
  7264.  
  7265. >Guido also posted a solution to comp.lang.perl. ( In python, that is. )
  7266.  
  7267. >You might also want to post your version ( and your comparison of 
  7268. >perl vs. python ).
  7269.  
  7270. I didn't post because I was interested in other people's solutions...
  7271. The best solution will make it to the next Python distribution.
  7272.  
  7273. >Then again, you might not. Some of the perl folks 
  7274. >seem to have pretty thin skin when it comes to criticism.
  7275.  
  7276. I can cope with that now...
  7277.  
  7278. >Another thread for discussion is:
  7279. > What is missing from python that is in perl. 
  7280. > ( Or library - but major features only, i.e. something that is a
  7281. >   real "application stopper" ! )
  7282. >
  7283. >My nomination: 
  7284. >  pack/unpack to/from binary structures ( for reading binary files )
  7285.  
  7286. You can read binary integers using ord() and <<, like the example I
  7287. gave in my mail about left shifts.  We use this at CWI to read and
  7288. write "AIFF" audio files (an EA-IFF subformat).  What else do you
  7289. need?
  7290.  
  7291. >  and the python equivalent of 'h2ph' to convert ".h" files into 
  7292. >  a "record description". 
  7293. >  ( I don't *think* there is a way to do this in python. Correct me
  7294. >    if I'm wrong. )
  7295.  
  7296. What's h2ph?  What's a "record description"?  (You can see I'm not a
  7297. Perl hacker :-)
  7298.  
  7299. >Any suggestions for a syntax for that routine any different than
  7300. >the perl equivalent ?  If we exclude longints, the major difference
  7301. >I see is dealing with possibly recursive structures: lists, tuples
  7302. >and dictionaries. Should the be disallowed ? ( Probably dictionaries
  7303. >should be disallowed. ) Should they require a nested format specification ?
  7304. >How about wildcard lengths for homogeneous sequences ? ( which might 
  7305. >suggest a 'length of previous/next sequence in bytes' specifier ? ) 
  7306.  
  7307. (This doesn't make sense to me until I know what h2ph is...)
  7308.  
  7309. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  7310. "May your mailbox overflow with messages" (modern Chinese curse)
  7311. 
  7312. 
  7313. Replied: Fri, 24 Jan 92 00:55:37 +0100
  7314. Replied: ""Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU> +inbox"
  7315. Received: by charon.cwi.nl with SMTP; Thu, 23 Jan 1992 20:09:10 +0100
  7316. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa01766;
  7317.           23 Jan 92 14:09 EST
  7318. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  7319.     id AA09952; Thu, 23 Jan 92 14:00:04 EST
  7320. Date: Thu, 23 Jan 92 14:00:04 EST
  7321. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  7322. Message-Id: <9201231900.AA09952@aemsun.med.Virginia.EDU>
  7323. To: python-list@cwi.nl
  7324. Subject: perl pack/unpack 
  7325.  
  7326.  
  7327. > >  and the python equivalent of 'h2ph' to convert ".h" files into 
  7328. > >  a "record description". 
  7329. > >  ( I don't *think* there is a way to do this in python. Correct me
  7330. > >    if I'm wrong. )
  7331. > What's h2ph?  What's a "record description"?  (You can see I'm not a
  7332. > Perl hacker :-)
  7333.  
  7334. h2ph converts C ".h" files into Perl ".ph" header files.
  7335. #define's & #ifdef's are converted into perl sub-routines.
  7336.  
  7337. pack/unpack convert to/from a perl-list <=> (binary) string 
  7338. according to a TEMPLATE that is sort of like a C or FORTRAN
  7339. format string. Something like:
  7340.  
  7341.  [ string, int, float1, float2,  float3 ] = unpack( binstr, 'a20Lf3' )
  7342.  
  7343.  
  7344. I'm not much of a Perl hacker, either. I was under the mistaken 
  7345. belief that h2ph also produced pack/unpack templates and/or subroutines
  7346. that converted binary C structs into Perl lists. I poked through the
  7347. output files in my /usr/local/lib/perl and could not find such a case. 
  7348. [ I was just starting to learn Perl when I discovered Python. I have 
  7349. only written a dozen or so simple perl scripts ( enough to get annoyed
  7350. with some of Perl's "features" ). *Tim* : As a beginner in both languages,
  7351. I can assert that the PYTHON version is MUCH more readable - you are 
  7352. obviously blinded by your experience with Perl! ) ]
  7353.  
  7354. >
  7355. > You can read binary integers using ord() and <<, like the example I
  7356. > gave in my mail about left shifts.  We use this at CWI to read and
  7357. > write "AIFF" audio files (an EA-IFF subformat).  What else do you
  7358. > need?
  7359.  
  7360. Thanks. I looked at lib/aiff.py.
  7361.  
  7362. I think pack/unpack is a convenient interface for the programmer.
  7363. ( But: can anyone suggest a better way? )
  7364.  
  7365. I could wrap up your conversion routines into a higher-level 
  7366. pack/unpack function. But it might be a reasonable thing to 
  7367. implement at a lower level. ( C ) - both for effeciency and for 
  7368. hiding some of the machine differences. [ I'm sure a major use
  7369. of this in perl is to build network packets of various sorts : 
  7370. you need to know if network byte order is the same as native 
  7371. byte order. ] 
  7372.  
  7373. The other point I wanted to raise was whether implementing a 
  7374. perl-like pack/unpack in python ( IN python OR C ) required/
  7375. suggested any restrictions or enhancements. The obvious restriction
  7376. is to require the input to be a "flat" list: i.e. not containing
  7377. other lists, tuples or dictionaries and only containing numbers 
  7378. and strings ( for this use, strings, although sequences, are 
  7379. considered simple types - we will not consider lists of strings as
  7380. sequences of sequences in this instance. ) 
  7381.  
  7382. [ It would not be difficult to make pack flatten it's input list arg, 
  7383. or perhaps better, to define a 'format' grammar that expresses 
  7384. nested-ness, but I think that those "conveniences" might prove to
  7385. be error-prone shortcuts. ]
  7386.  
  7387.  
  7388. The other thing I believe Perl has, is more elaborate output 
  7389. formatting & report generation capabilities. But I haven't 
  7390. actually ever got that far in my Perl programming, so I'm 
  7391. taking that rumour at face value. 
  7392.  
  7393. Some of the people in the comp.lang.perl discussion expressed a 
  7394. wish for a "Perl-2". I would hope that some constructive criticism
  7395. from the hard core Perl faithful would be "this (x) is what is lacking
  7396. from Python for me to consider it a real substitute for Perl" 
  7397. ( and *NOT* just "I happen to like "@_" , etc. - why use 8 chars when 
  7398.  2 will do!", and obviously not compatability questions.  )
  7399.  
  7400.  
  7401.  
  7402.  
  7403.  
  7404. Follows (LONG) perl pack/unpack man section: 
  7405.  
  7406.  
  7407.  
  7408.  
  7409.  
  7410.         pack(TEMPLATE,LIST)
  7411.  
  7412.  
  7413. Takes an array or list of values  and  packs  it  into  a  binary
  7414. structure,  returning  the  string containing the structure.  The
  7415. TEMPLATE is a sequence of characters that give the order and type
  7416. of values, as follows:
  7417.  
  7418.         A       An ascii string, will be space padded.
  7419.         a       An ascii string, will be null padded.
  7420.         c       A signed char value.
  7421.         C       An unsigned char value.
  7422.         s       A signed short value.
  7423.         S       An unsigned short value.
  7424.         i       A signed integer value.
  7425.         I       An unsigned integer value.
  7426.         l       A signed long value.
  7427.         L       An unsigned long value.
  7428.         n       A short in network order.
  7429.         N       A long in network order.
  7430.         f       A single-precision float in the native format.
  7431.         d       A double-precision float in the native format.
  7432.         p       A pointer to a string.
  7433.         x       A null byte.
  7434.         X       Back up a byte.
  7435.         @       Null fill to absolute position.
  7436.         u       A uuencoded string.
  7437.         b       A bit string (ascending bit order, like vec()).
  7438.         B       A bit string (descending bit order).
  7439.         h       A hex string (low nybble first).
  7440.         H       A hex string (high nybble first).
  7441.  
  7442. Each letter may optionally be followed by a number which gives  a
  7443. repeat  count.  With all types except "a", "A", "b", "B", "h" and
  7444. "H", the pack function will gobble up that many values  from  the
  7445. LIST.   A  * for the repeat count means to use however many items
  7446. are left.  The "a" and "A" types gobble just one value, but  pack
  7447. it  as  a string of length count, padding with nulls or spaces as
  7448. necessary.  (When  unpacking,  "A"  strips  trailing  spaces  and
  7449. nulls, but "a" does not.) Likewise, the "b" and "B" fields pack a
  7450. string that many bits long.  The "h" and "H" fields pack a string
  7451. that many nybbles long.  Real numbers (floats and doubles) are in
  7452. the native machine format only; due to the multiplicity of float-
  7453. ing  formats around, and the lack of a standard network represen-
  7454. tation, no facility for interchange has been  made.   This  means
  7455. that packed floating point data written on one machine may not be
  7456. readable on another - even if both use IEEE floating point arith-
  7457. metic  (as  the  endian-ness  of the memory representation is not
  7458. part of the IEEE spec).  Note that perl uses  doubles  internally
  7459. for  all numeric calculation, and converting from double -> float
  7460. -> double will lose precision (i.e. unpack("f", pack("f",  $foo))
  7461. will not in general equal $foo).
  7462. Examples:
  7463.  
  7464.         $foo = pack("cccc",65,66,67,68);
  7465.         # foo eq "ABCD"
  7466.         $foo = pack("c4",65,66,67,68);
  7467.         # same thing
  7468.  
  7469.         $foo = pack("ccxxcc",65,66,67,68);
  7470.         # foo eq "AB\0\0CD"
  7471.  
  7472.         $foo = pack("s2",1,2);
  7473.         # "\1\0\2\0" on little-endian
  7474.         # "\0\1\0\2" on big-endian
  7475.  
  7476.         $foo = pack("a4","abcd","x","y","z");
  7477.         # "abcd"
  7478.  
  7479.         $foo = pack("aaaa","abcd","x","y","z");
  7480.         # "axyz"
  7481.  
  7482.         $foo = pack("a14","abcdefg");
  7483.         # "abcdefg\0\0\0\0\0\0\0"
  7484.  
  7485.         $foo = pack("i9pl", gmtime);
  7486.         # a real struct tm (on my system anyway)
  7487.  
  7488.         sub bintodec {
  7489.             unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
  7490.         }
  7491. The same template may generally also be used in the unpack  func-
  7492. tion.
  7493.  
  7494.  
  7495.  
  7496.  
  7497.  
  7498.  
  7499.         unpack(TEMPLATE,EXPR)
  7500.  
  7501.  
  7502. Unpack does the reverse of pack: it takes a string representing a
  7503. structure  and  expands it out into an array value, returning the
  7504. array value.  (In a scalar context, it merely returns  the  first
  7505. value  produced.) The TEMPLATE has the same format as in the pack
  7506. function.  Here's a subroutine that does substring:
  7507.  
  7508.         sub substr {
  7509.                 local($what,$where,$howmuch) = @_;
  7510.                 unpack("x$where a$howmuch", $what);
  7511.         }
  7512.  
  7513. and then there's
  7514.  
  7515.         sub ord { unpack("c",$_[0]); }
  7516.  
  7517. In addition, you may prefix a field with a %<number> to  indicate
  7518. that you want a <number>-bit checksum of the items instead of the
  7519. items themselves.  Default is a 16-bit  checksum.   For  example,
  7520. the  following  computes the same number as the System V sum pro-
  7521. gram:
  7522.  
  7523.         while (<>) {
  7524.             $checksum += unpack("%16C*", $_);
  7525.         }
  7526.         $checksum %= 65536;
  7527.  
  7528.  
  7529.  
  7530.  
  7531.  
  7532.  
  7533.  
  7534.  
  7535.  
  7536.  
  7537.  
  7538.  
  7539.  
  7540. 
  7541. 
  7542. Received: by charon.cwi.nl with SMTP; Fri, 24 Jan 1992 10:35:56 +0100
  7543. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  7544.     (5.61/UUNET-internet-primary) id AA29535; Fri, 24 Jan 92 04:35:50 -0500
  7545. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  7546.     (queueing-rmail) id 043141.5556; Fri, 24 Jan 1992 04:31:41 EST
  7547. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  7548.     id AA15733; Fri, 24 Jan 92 04:25:27 EST
  7549. Received: by kaos.ksr.com (4.0/SMI-3.2)
  7550.     id AA19265; Fri, 24 Jan 92 04:25:26 EST
  7551. Message-Id: <9201240925.AA19265@kaos.ksr.com>
  7552. To: sdm7g@Virginia.EDU
  7553. Subject: Re: perl pack/unpack
  7554. Cc: python-list@cwi.nl
  7555. Date: Fri, 24 Jan 92 04:25:25 EST
  7556. From: Tim Peters <tim@ksr.com>
  7557.  
  7558. >  [steve m]
  7559. >  ... I was under the mistaken belief that h2ph also produced
  7560. >  pack/unpack templates and/or subroutines that converted binary C
  7561. >  structs into Perl lists. ...
  7562.  
  7563. h2ph does what it does, & that's about the only definition you can count
  7564. on <frown> -- generally does a reasonable translation of #if[def],
  7565. #define of literal constants, & #define of simple macros.  Nearly
  7566. everything else (structs, typedefs, declarations, non-trivial macros, is
  7567. ...) ignored, or mis-translated into Perl that would blow up if it
  7568. were invoked.  Provided you don't expect much from it, it's very useful
  7569. for getting at the simple stuff.  It would be easy to whip up something
  7570. comparable for Python, but one of the secret pitfalls of using a
  7571. stronger language is that you're not inclined to settle for doing as
  7572. limited a job <0.7 grin>.
  7573.  
  7574. >  ... *Tim* : As a beginner in both languages, I can assert that the
  7575. >  PYTHON version is MUCH more readable - you are obviously blinded by
  7576. >  your experience with Perl!
  7577.  
  7578. Sorry, Steve, I did a poor job of writing there.  I fully agree that
  7579. Python is both much easier to learn & much easier to read than (typical)
  7580. Perl (just "typical" because you don't *have* to use Perl's bottomless
  7581. bag of hidden tricks -- think it's quite possible to write Perl that's
  7582. as readable as C, it's just that nobody does ...).
  7583.  
  7584. I claimed that my program was as hard to *"understand"* as the Perl
  7585. version, and understanding covers much more than readability.  I think
  7586. two things conspire to make the Python version hard to understand:
  7587.  
  7588. 1) Felix was disingenuous in calling the problem "very simple" -- it's
  7589.    quite involved when you get into the details, and is prone to subtle
  7590.    bugs (e.g., the Perl version had several bugs; if you give Guido's
  7591.    version more than one directory, it may not sort them correctly at
  7592.    the *topmost* level; & I'm not admitting to the bugs in my version
  7593.    <grin>).  So much of the "hard to understand" comes from the problem
  7594.    itself.  And it may not be apparent from looking at a solution, but
  7595.    Python has a strong advantage here in that it supports the kinds
  7596.    of data types that more-or-less naturally spring to mind; but that's
  7597.    an advantage for the progammer more than it is for the reader.  Which
  7598.    leads to ...
  7599.  
  7600. 2) In my experience, it takes a lot of time for sometime to get
  7601.    comfortable with reading code that uses recursive data types in a
  7602.    language without declarations & in programs that don't document what
  7603.    the data types are.  The Perl version was hideously obscure because
  7604.    you just can't tell what "*kid" is being used for without
  7605.    understanding the logic completely first; the Python versions were
  7606.    similarly obscure (Guido's version was certainly better than mine in
  7607.    this respect) in using data types that weren't *obvious* from the
  7608.    text.  You really have to dig into the logic to figure out the
  7609.    precise inner structure of the various incestuous lists, tuples &
  7610.    dictionaries.  I figure the best solution to that is good comments,
  7611.    but you can't do that in this example without making it *look* much
  7612.    bigger than the (uncommented) Perl version <0.9 grin>.
  7613.  
  7614. >  ... [skipping over pack/unpack] ...
  7615.  
  7616. >  The other thing I believe Perl has, is more elaborate output
  7617. >  formatting & report generation capabilities. But I haven't
  7618. >  actually ever got that far in my Perl programming, so I'm
  7619. >  taking that rumour at face value.
  7620.  
  7621. Perl started life as a report-generating language, & has a very nice
  7622. "format" feature (+ lots of associated hair) to support that function
  7623. (in essence, you draw a picture of what you want the report to look
  7624. like, & Perl takes care of justification & centering & rounding etc the
  7625. data to match the picture).
  7626.  
  7627. Perl has many features "like that":  unusual hacks that most people never
  7628. use but some people use very heavily.  Transparent binding of dbm files
  7629. to associative arrays is another of that ilk; for that matter, so are
  7630. pack/unpack; the dataflow mechanisms for tracking "unsafe" data derived
  7631. from the environment is another; ... Perl simply can't be beat for short
  7632. programs that rely on one of those built-in pieces of magic.
  7633.  
  7634. seeing-python's-strengths-as-being-overwhelming-but-in-different-areas-ly
  7635.    y'rs  - tim
  7636.  
  7637. Tim Peters   Kendall Square Research Corp
  7638. tim@ksr.com,         ksr!tim@uunet.uu.net
  7639. 
  7640. 
  7641. Received: by charon.cwi.nl with SMTP; Tue, 28 Jan 1992 05:32:10 +0100
  7642. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  7643.     (5.61/UUNET-internet-primary) id AA29023; Mon, 27 Jan 92 23:32:16 -0500
  7644. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  7645.     (queueing-rmail) id 233138.12555; Mon, 27 Jan 1992 23:31:38 EST
  7646. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  7647.     id AA15614; Mon, 27 Jan 92 23:08:10 EST
  7648. Received: by kaos.ksr.com (4.0/SMI-3.2)
  7649.     id AA27911; Mon, 27 Jan 92 23:08:09 EST
  7650. Message-Id: <9201280408.AA27911@kaos.ksr.com>
  7651. To: python-list@cwi.nl
  7652. Subject: One more time on dutree ...
  7653. Date: Mon, 27 Jan 92 23:08:08 EST
  7654. From: Tim Peters <tim@ksr.com>
  7655.  
  7656. Posted the following to comp.lang.perl; figure there's enough interest
  7657. in the Python world to justify sending it to python-list too; feel free
  7658. to scream if you disagree.
  7659.  
  7660. tersely y'rs  - tim
  7661.  
  7662. Newsgroups: comp.lang.perl
  7663. Subject: Re: The problems of Perl (Re: Question (silly?))
  7664. Summary: use the right tool for the job <grin>
  7665. Message-ID: <9170@ksr.com>
  7666. Date: 27 Jan 92 22:41:16 EST
  7667. References: <=#Hues+4@cs.psu.edu> <1992Jan17.053115.4220@convex.com> <5230@charon.cwi.nl> <1992Jan27.020608.5983@convex.com>
  7668.  
  7669. In article <1992Jan27.020608.5983@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
  7670. >...
  7671. >Larry mentioned to me offline last week that all it takes for this task
  7672. >is getting the right sort criteria, and that you needn't use recursion
  7673. >or string-munging trickery (the two approaches we've seen) to do it.
  7674.  
  7675. By assigning a unique (but otherwise arbitrary) integer ID to each group
  7676. of siblings, and using that as the primary sort key (and the du-reported
  7677. size as a secondary sort key), only one sort step is needed.  However,
  7678. it leaves the records in a bad order for printing (because it makes
  7679. siblings adjacent even if they in turn have children).  With more
  7680. trickery, a second (stable) sort could fix that.
  7681.  
  7682. The parent-child relationships can be captured without recursion and
  7683. without string comparisons by noting that:
  7684.  
  7685. 1) The output of "du dir" is a postorder traversal of the subtree rooted
  7686.    at "dir".
  7687.  
  7688. 2) The number of components in a directory's pathname reliably
  7689.    identifies the depth of the directory.
  7690.  
  7691. Those clues can be used to build up the tree in a single non-recursive
  7692. scan of du's output, just comparing the number of components from one
  7693. line to the next.
  7694.  
  7695. >Perhaps someone would care to post a solution using that method.
  7696.  
  7697. Will attach one that uses recursion only in the output routine & that
  7698. restricts itself to "flat" (non-recursive) data types.  Even so, I wrote
  7699. it in Python because it's still clumsier in Perl.  And I think this was
  7700. Felix Lee's original point:  while it's amusing to think of ways to twist
  7701. the problem to fit comfortably into Perl's repertoire, the *obvious* way
  7702. to attack a tree-based algorithm is to use recursive data types, so that
  7703. trying to use a language that doesn't support them changes the focus of
  7704. the effort from solving the problem to wrestling with the language.  For
  7705. this reason, as an old-time Fortran programmer I am sometimes overcome
  7706. by deja vu when using Perl <0.7 grin>.
  7707.  
  7708. BTW, this is not intended as a knock at Perl!  I use Perl dozens of
  7709. times each day & believe it's superbly suited to many real-world tasks.
  7710. There are, however, many real-world tasks (like this one) for which Perl
  7711. is about the last tool I'd choose.
  7712.  
  7713.  
  7714. Some notes on the code below:
  7715.  
  7716.   - This inserts a '.' entry as needed to suck up otherwise-unaccounted-
  7717.     for space.
  7718.  
  7719.   - The paths at the top level are printed out in full.
  7720.  
  7721.   - It works correctly if more than one directory is given on the
  7722.     command line (Tom's original program printed only the last one in
  7723.     this case, and Guido's sometimes got the sorting wrong at (just) the
  7724.     topmost level in this case).
  7725.  
  7726.   - When two or more siblings have the same size, this version prints
  7727.     them in alphabetical order.  This was my intent, but you won't see
  7728.     any code specifically arranging for that to happen -- it happens by
  7729.     magic because of the way Python defines ordering on tuples.
  7730.  
  7731.   - Unlike the other programs, this one does *not* try to vary the
  7732.     indentation depending on the length of a component name.  I simply
  7733.     dislike the way the output looks when that's done, and much prefer a
  7734.     fixed indentation increment.
  7735.  
  7736.   - The sorting is done as it goes along (rather than using the trick
  7737.     mentioned above to do it all at once) just because that's the most
  7738.     natural way to do it; it would be clumsier to push the sorting off
  7739.     to the end.
  7740.  
  7741.   - The primary data structure is the associative array 'kids', which
  7742.     has type (making up a type-decl notation here ...)
  7743.  
  7744.     array[string] of list of triple(int,string,string)
  7745.  
  7746.     I know it isn't hard to fake such a thing in Perl, but it does take
  7747.     some tedious trickery not needed in Python.  That's the "deep"
  7748.     reason I think Python (or Icon) is a better tool for this problem:
  7749.     it consumes less of *my* time to get the job done (& I prefer Perl
  7750.     for problems where Perl is less hassle ...).
  7751.  
  7752. if-i-wanted-speed-i'd-write-it-in-fortran<grin>-ly y'rs  - tim
  7753.  
  7754. Tim Peters   Kendall Square Research Corp
  7755. tim@ksr.com,         ksr!tim@uunet.uu.net
  7756.  
  7757. #!/usr/local/bin/python
  7758. import string, posix, sys
  7759.  
  7760. leadering = '      |'
  7761.  
  7762. def suck_up_du( dir ):
  7763.     # 'kids' maps a pathname to a flat list of the pathname's children;
  7764.     # each entry in the list is a triple:
  7765.     #     size of child (negated so it sorts in decreasing order)
  7766.     #      last component of child's pathname (cached for output)
  7767.     #      child's full pathname (used to index back into 'kids')
  7768.     global kids
  7769.  
  7770.     a, total, leftover, oldncomps = [], 0, {}, 0
  7771.     for line in posix.popen( 'du ' + dir, 'r' ).readlines():
  7772.     [size, path] = string.split( line )
  7773.     size = eval(size) # convert to int
  7774.     comps = string.splitfields( path, '/' )
  7775.     ncomps = len(comps)
  7776.     if ncomps < oldncomps:  # line is parent
  7777.         if total < size: a.append( (total-size, '.', path+'/.') )
  7778.         a.sort()
  7779.         kids[path] = a
  7780.         oldncomps = ncomps
  7781.         try: a, total = leftover[`ncomps`]
  7782.         except KeyError: a, total = [], 0
  7783.         leftover[`ncomps`] = [], 0
  7784.     elif ncomps > oldncomps:  # line starts new subtree
  7785.         leftover[`oldncomps`] = a, total
  7786.         a, total, oldncomps = [], 0, ncomps
  7787.     a.append( (-size, comps[-1], path) ) # negate size for sort
  7788.     total = total + size
  7789.     return a[0]
  7790.  
  7791. def dump( prefix, list ):
  7792.     for size, last, path in list:
  7793.     print prefix + string.rjust(`-size`,lenlead), last
  7794.     if kids.has_key(path): dump( prefix + leadering, kids[path] )
  7795.  
  7796. def dudump( dirlist ):
  7797.     global kids, lenlead
  7798.     kids, a, lenlead = {}, [], len(leadering)
  7799.     for dir in dirlist:
  7800.     size, junk, path = suck_up_du( dir )
  7801.     a.append( (size,path,path) ) # display full path at top level
  7802.     a.sort()
  7803.     dump( '', a )
  7804.  
  7805. dudump( sys.argv[1:] or [''] )
  7806.  
  7807. >>> END OF MSG
  7808. 
  7809. 
  7810. Received: by charon.cwi.nl with SMTP; Thu, 30 Jan 1992 22:56:54 +0100
  7811. Received: by relay.CDNnet.CA (4.1/1.14)
  7812.     id AA20620; Thu, 30 Jan 92 13:56:11 PST
  7813. Date: 30 Jan 92 13:50 -0800
  7814. From: John Buchanan <juancho@cs.ubc.ca>
  7815. To: <python-list@cwi.nl>
  7816. Message-Id: <1783*juancho@cs.ubc.ca>
  7817. Subject: installing python 0.9.4alpha
  7818.  
  7819.  
  7820. I am not sure whether this goes here on in some other list but what the heck.
  7821.  
  7822. I got the python and stdwin archives from wuarchive.wustl.edu.  They
  7823. installed very nicely on sgi sparc architectures.  They also installed on an
  7824. IBM but with a little of work on the stdwin side.
  7825.  
  7826. In the Conf directory of the stdwin directory structure I had to make a new file
  7827. proto.arch.aix and I had to place a dummy 'arch' program in my path.  After
  7828. I did this installing with 
  7829.     ./mkmf -o aix
  7830.  
  7831. worked nicely.  
  7832. 
  7833. 
  7834. Received: by charon.cwi.nl with SMTP; Tue, 4 Feb 1992 06:32:20 +0100
  7835. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  7836.     (5.61/UUNET-internet-primary) id AA14254; Tue, 4 Feb 92 00:32:25 -0500
  7837. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  7838.     (queueing-rmail) id 003133.29439; Tue, 4 Feb 1992 00:31:33 EST
  7839. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  7840.     id AA15056; Mon, 3 Feb 92 22:32:25 EST
  7841. Received: by kaos.ksr.com (4.0/SMI-3.2)
  7842.     id AA27065; Mon, 3 Feb 92 22:32:24 EST
  7843. Message-Id: <9202040332.AA27065@kaos.ksr.com>
  7844. To: python-list@cwi.nl
  7845. Subject: An improved Emacs Python mode
  7846. Date: Mon, 03 Feb 92 22:32:24 EST
  7847. From: Tim Peters <tim@ksr.com>
  7848.  
  7849. You might want to give the following a try in place of
  7850. Python/misc/python.el.
  7851.  
  7852. Major enhancements:
  7853.  
  7854.   - Honors your choice of indentation increment.
  7855.  
  7856.   - Less naive about when to indent.  E.g., will not indent after
  7857.     a = b   # ok: # found
  7858.     or
  7859.     a = ': #'
  7860.     In the absence of continuation lines, I don't believe this version
  7861.     will ever get it wrong (so please send an example if it does).
  7862.  
  7863. Details:
  7864.  
  7865.   - Suggest you install this as python-mode.el (the Python distribution
  7866.     names it 'python.el', but the '-mode' suffix is a universal
  7867.     convention for Emacs language modes).  This will require that you
  7868.     change your .emacs to say
  7869.     (autoload 'python-mode "python-mode" "" t)
  7870.     instead of the current
  7871.     (autoload 'python-mode "python" "" t)
  7872.  
  7873.   - As written, the indentation increment defaults to 4 columns, just
  7874.     because that's what I seem to like best for Python code.  Change the
  7875.     variable py-indent-offset to whatever you want it to be; e.g., to be
  7876.     compatible with the current python.el, which always indents by full
  7877.     tabs, stick this in your .emacs file:
  7878.     (setq py-indent-offset 8)
  7879.  
  7880.   - "Unindenting" a line is still done via the DEL key, but the DEL key
  7881.     is now smarter:  it will "dedent" the line by py-indent-offset
  7882.     columns if point happens to be at the first non-blank character of a
  7883.     line (or at the end of an entirely blank line), else it will delete
  7884.     just the preceding character (& convert tabs to spaces if needed, so
  7885.     that just 1 column position will be deleted).
  7886.  
  7887.     I worried that this interface might be too much of a hack, but in
  7888.     practice I've found it to be quite natural.  If you have a better
  7889.     idea, or find it too surprising or confusing, please holler.
  7890.  
  7891.   - Still no effective support for continuation lines (this is all
  7892.     driven by what bugs me when I'm writing code, & I simply haven't had
  7893.     a need for a continuation line yet hence didn't worry about 'em).
  7894.  
  7895.   - Changed the mode documentation (C-h m) to warn against using
  7896.     indent-region, and to suggest using indent-rigidly for shifting a
  7897.     block of Python code.  One thing I would like to do is add some
  7898.     shortcut support for marking & shifting regions of Python code;
  7899.     indent-region will never be able to figure out what you wanted
  7900.     (think about it <grin>), so after cutting a piece of Python code out
  7901.     of one file & pasting it into a new context, it can be quite a pain
  7902.     to figure out how to get the indentation right for the new context.
  7903.     It should be possible for Python-mode to give the user some help
  7904.     with this (e.g., something as simple as "'C-c <' shifts the region
  7905.     left by py-indent-offset columns, and 'C-c >' shifts it right
  7906.     similarly" would, I think, be helpful).
  7907.  
  7908. If there's something that you think would help you often & a lot in
  7909. Python mode, drop me a note & maybe I'll find the time to try & hack it
  7910. in.
  7911.  
  7912. just-another-aggressive-member-of-the-python-youth-corps-ly y'rs  - tim
  7913.  
  7914. Tim Peters   Kendall Square Research Corp
  7915. tim@ksr.com,         ksr!tim@uunet.uu.net
  7916.  
  7917. ;; Major mode for editing Python programs.
  7918. ;; by: Michael A. Guravage and Guido van Rossum <guido@cwi.nl>
  7919. ;;
  7920. ;; The following statements, placed in your .emacs file or site-init.el,
  7921. ;; will cause this file to be autoloaded, and python-mode invoked, when
  7922. ;; visiting .py files (assuming the file is in your load-path):
  7923. ;;
  7924. ;;    (autoload 'python-mode "python-mode" "" t)
  7925. ;;    (setq auto-mode-alist
  7926. ;;          (cons '("\\.py$" . python-mode) auto-mode-alist))
  7927.  
  7928. ;; Change log:
  7929. ;
  7930. ; Sun Feb  2 02:08:47 1992  tim
  7931. ; added support for user-defined indentation increment:
  7932. ;     added python-indent variable
  7933. ;    changed py-indent-line to indent python-indent columns
  7934. ;    fixed small bug in py-indent-line (changed start of r.e.
  7935. ;        from [^#] to [^#\n])
  7936. ;    added py-delete-char function; bound to \177
  7937. ;    changed mode blurb accordingly
  7938. ;
  7939. ; Sun Feb  2 21:48:59 1992  tim
  7940. ; renamed 'python-indent' to 'py-indent-offset' for internal consistency
  7941. ; replaced regexp in py-indent-line so it no longer indents after
  7942. ;    lines like:
  7943. ;    a = b   # ok: # found
  7944. ;    a = ': #'
  7945. ;
  7946. ; Mon Feb  3 20:37:27 1992  tim
  7947. ; renamed file to 'python-mode.el' for consistency with other Emacs
  7948. ;    language modes; changed autoload instructions accordingly
  7949. ; improved accuracy of new/changed docs
  7950. ; changed py-python-command from defconst to defvar so .emacs can
  7951. ;    override it if desired
  7952. ; add warning about indent-region; suggest indent-rigidly
  7953.  
  7954. (provide 'python)
  7955.  
  7956. ;; Constants and variables
  7957.  
  7958. (defvar py-python-command "python"
  7959.   "*UNIX shell command used to start Python interpreter")
  7960.  
  7961. (defvar py-indent-offset 4
  7962.   "*Indentation increment in Python mode")
  7963.  
  7964. (defvar py-mode-map nil
  7965.   "Keymap used in Python mode buffers")
  7966.  
  7967. (defvar py-mode-syntax-table nil
  7968.   "Python mode syntax table")
  7969.  
  7970.  
  7971. ;; Initialize the keymap if it doesn't already exist
  7972.  
  7973. (if (null py-mode-map)
  7974.     (progn
  7975.       (setq py-mode-map (make-sparse-keymap))
  7976.       (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
  7977.       (define-key py-mode-map "\C-c|"     'py-execute-region)
  7978.       (define-key py-mode-map "\C-c!"     'py-shell)
  7979.       (define-key py-mode-map "\177"     'py-delete-char)))
  7980.  
  7981. ;; General Functions
  7982.  
  7983. (defun python-mode nil
  7984.   "Major mode for editing Python files.
  7985.  
  7986. \\[py-execute-buffer] sends the entire buffer to the Python interpreter.
  7987. \\[py-execute-region] sends the current region.
  7988. \\[py-shell] starts a Python interpreter window; this will be used by
  7989. subsequent \\[py-execute-buffer] or \\[py-execute-region] commands.
  7990.  
  7991.  
  7992. Variable py-indent-offset is the indentation increment.
  7993.  
  7994. \\[newline-and-indent] indents by an extra py-indent-offset columns where
  7995. necessary.  Use \\[py-delete-char] to reduce indentation.
  7996.  
  7997. \\[py-delete-char] reduces the indentation of a line by py-indent-offset columns
  7998. if point is at the first non-blank character (if any) of a line, or at
  7999. the last character of an entirely blank line; else it deletes the
  8000. preceding character, converting tabs to spaces as needed so that only
  8001. one character position is deleted.
  8002.  
  8003. Warning:  indent-region should not be used!  Use indent-rigidly to shift
  8004. regions of Python code instead.  indent-region can't guess the
  8005. indentation you had in mind, and will, e.g., change
  8006.  
  8007.     xxx
  8008.     if a = b:
  8009.         c = d
  8010.     if e = f: print 'ouch!'
  8011.  
  8012. to
  8013.     xxx
  8014.     if a = b:
  8015.         c = d
  8016.         if e = f: print 'ouch!'
  8017.  
  8018. Paragraphs are separated by blank lines only.
  8019. \\{py-mode-map}
  8020. \\[python-mode] calls the value of the variable py-mode-hook with no args,
  8021. if that value is non-nil."
  8022.   (interactive)
  8023.   (kill-all-local-variables)
  8024.   (use-local-map py-mode-map)
  8025.   (setq major-mode 'python-mode)
  8026.   (setq mode-name "Python")
  8027.   (if py-mode-syntax-table
  8028.       (set-syntax-table py-mode-syntax-table)
  8029.     (progn
  8030.       (setq py-mode-syntax-table (make-syntax-table))
  8031.       (set-syntax-table py-mode-syntax-table)
  8032.       (modify-syntax-entry ?\( "()")
  8033.       (modify-syntax-entry ?\) ")(")
  8034.       (modify-syntax-entry ?\[ "(]")
  8035.       (modify-syntax-entry ?\] ")[")
  8036.       (modify-syntax-entry ?\{ "(}")
  8037.       (modify-syntax-entry ?\} "){")
  8038.       (modify-syntax-entry ?\_ "w")
  8039.       (modify-syntax-entry ?\' "\"")    ; single quote is string quote
  8040.       (modify-syntax-entry ?\` "$")    ; backquote is open and close paren
  8041.       (modify-syntax-entry ?\# "<")    ; hash starts comment
  8042.       (modify-syntax-entry ?\n ">")    ; newline ends comment
  8043.       ))
  8044.   (make-local-variable 'paragraph-separate)
  8045.   (setq paragraph-separate "^[ \t\f]*$")
  8046.   (make-local-variable 'paragraph-start)
  8047.   (setq paragraph-start "^[ \t\f]*$")
  8048.   (make-local-variable 'require-final-newline)
  8049.   (setq require-final-newline t)
  8050.   (make-local-variable 'comment-start)
  8051.   (setq comment-start "# ")
  8052.   (make-local-variable 'comment-start-skip)
  8053.   (setq comment-start-skip "# *")
  8054.   (make-local-variable 'comment-column)
  8055.   (setq comment-column 40)
  8056.   (make-local-variable 'indent-line-function)
  8057.   (setq indent-line-function 'py-indent-line)
  8058.   (run-hooks 'py-mode-hook))
  8059.  
  8060.  
  8061. ;; Functions that execute Python commands in a subprocess
  8062.  
  8063. (defun py-shell nil
  8064.   "Start an interactive Python interpreter in another window.
  8065. The variable py-python-command names the interpreter."
  8066.   (interactive)
  8067.   (require 'shell)
  8068.   (switch-to-buffer-other-window
  8069.    (make-shell "Python" py-python-command))
  8070.   (make-local-variable 'shell-prompt-pattern)
  8071.   (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. "))
  8072.  
  8073. (defun py-execute-region (start end)
  8074.   "Send the region between START and END to a Python interpreter.
  8075. If there is a *Python* process it is used."
  8076.   (interactive "r")
  8077.   (condition-case nil
  8078.       (process-send-string "Python" (buffer-substring start end))
  8079.     (error (shell-command-on-region start end py-python-command nil))))
  8080.  
  8081. (defun py-execute-buffer nil
  8082.   "Send the contents of the buffer to a Python interpreter.
  8083. If there is a *Python* process buffer it is used."
  8084.   (interactive)
  8085.   (py-execute-region (point-min) (point-max)))
  8086.  
  8087.  
  8088. ;; Functions for Python style indentation
  8089.  
  8090. (defun py-indent-line nil
  8091.   "Fix the indentation of the current line according to Python rules."
  8092.   (interactive)
  8093.   (backward-to-indentation 1)
  8094.   (let ( (ci (current-indentation)) )
  8095.     (if (looking-at
  8096.      "\\([^#'\n]\\|'\\([^'\n\\\\]\\|\\\\.\\)*'\\)*:[ \t]*\\(#.*\\)?$")
  8097.     (setq ci (+ ci py-indent-offset)))
  8098.     (forward-line 1)
  8099.     (delete-horizontal-space)
  8100.     (indent-to ci)))
  8101.  
  8102. (defun py-delete-char nil
  8103.   "If at first non-blank character, or at last character of blank line,
  8104. reduce indentation by the value of variable py-indent-offset.  Else
  8105. delete preceding character, converting tabs to spaces."
  8106.   (interactive)
  8107.   (backward-delete-char-untabify
  8108.    (if (and
  8109.     (= (current-indentation) (current-column))
  8110.     (>= (current-indentation) py-indent-offset))
  8111.        py-indent-offset
  8112.      1)))
  8113.  
  8114. ;; To do:
  8115. ;; - proper indentation for continuation lines
  8116. ;; - add a newline when executing buffer ending in partial line
  8117. ;; - suppress prompts when executing regions
  8118. ;; - switch back to previous buffer when starting shell
  8119. ;; - support for ptags
  8120.  
  8121. >>> END OF MSG
  8122. 
  8123. 
  8124. Replied: Wed, 05 Feb 1992 00:14:20 +0100
  8125. Replied: python-list@cwi.nl
  8126. Received: by charon.cwi.nl with SMTP; Tue, 4 Feb 1992 22:05:59 +0100
  8127. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa29078;
  8128.           4 Feb 92 16:05 EST
  8129. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  8130.     id AA05826; Tue, 4 Feb 92 15:56:25 EST
  8131. Date: Tue, 4 Feb 92 15:56:25 EST
  8132. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  8133. Message-Id: <9202042056.AA05826@aemsun.med.Virginia.EDU>
  8134. To: python-list@cwi.nl
  8135. Subject: Python portability planning ( and prototypes ) [ pwas: pack/unpack...]
  8136.  
  8137. In discussion with guido ( mostly about pack/unpack from binary records )
  8138. I suggested a sys.arch variable be added to module sys to contain machine
  8139. or os specific info. 
  8140.  
  8141. [ Guido: I DO have some more specific followups on some of the other
  8142.   questions/issues, but I, too, have been busy with other things. I've 
  8143.   been sitting on this os module waiting for time to move all the sources
  8144.   to my PC and start on the dos end of things. ]
  8145.  
  8146.  
  8147. me> The pack/unpack proposal was just meant as something for discussion. 
  8148. me> I am now serious about 'sys.arch' as a proposal. 
  8149.  
  8150. To which guido responded:
  8151.  
  8152. Guido> But what value would you like sys.arch to have, and how would you
  8153. Guido> choose the right value at compile time?  An an alternate proposal, I
  8154. Guido> can implement an interface to the Posix uname() function; this returns
  8155. Guido> a system name, a node (host) name, a release level, a version level
  8156. Guido> (of the release), and a machine type.  I suppose this is easy enough
  8157. Guido> to fake for systems that don't have full Posix libraries yet.  For
  8158. Guido> now, you can fake this by doing something like
  8159. Guido> string.split(posix.popen('uname -a', 'r')), provided you have the
  8160. Guido> uname program.  Older systems may have a utility called arch (SunOS)
  8161. Guido> or machine (Ultrix), I'm sure you can cook up a function which tries
  8162. Guido> several of these until it finds one which works.
  8163.  
  8164. Well, that's the reason I brought up the subject. I *DIDN'T* in any
  8165. sense mean to infer that most of these things couldn't be done in python
  8166. ( rather than coded in C to provide a python-interface ), but since they
  8167. all address portability concerns, they seem to indicate to me that 
  8168. EITHER they should be "builtins" to the language ( and defined in the 
  8169. library manual ) OR they should be declared "conventions" ( i.e. "this
  8170. is the proper way to do this in python, and therefore you had better not
  8171. do *X* because that will make your code horribly un-portable." )
  8172.  
  8173. So, I'm only partially asking a "how-to" question. 
  8174. The more important question is the "how-should" question. 
  8175.  
  8176.  I am also *not* discussing things that should be C-source coded ( rather 
  8177. than Python-source coded ) for effeciency. There probably are some cases 
  8178. but there it makes sense to try out a python prototype BEFORE considering 
  8179. a "native code" solution.
  8180.  
  8181. But there are some other things that either are not do-able *directly*
  8182. in python, or are relatively difficult to provide in python, but easier
  8183. to provide in C-source as python "built-in's". 
  8184.  
  8185.  The base level determination now seems to be that you can tell (roughly)
  8186. what os you are on by which "import" statement succeeds. [ *Is* this the
  8187. only way ? I think so. ]. If "import mac" succeeds, the we know we are
  8188. on a  mac, and can assume that the CPU is a m680x0 type, and we can 
  8189. have a dictionary of CPU's and endian-ness-flags, etc. But if/when apple
  8190. ships Risc-Mac's the library routines need to be changed. It doesn't seem
  8191. any more of a bother to require some #define's/#ifdef's in a config.h 
  8192. file that pre-define some of these. Using the 'succesful import' rule 
  8193. for determining machine variables also means that the convention must 
  8194. be enforced of "no modules named "dos", "mac", "posix" , etc. This means
  8195. that you can't provide (limited) posix or dos functionality to another
  8196. system by an "external" posix module ( or dos.py ) because that will 
  8197. make 'import posix' succeed on the wrong machine, and may interfere with
  8198. other routines that need it to fail to determine machine specific parameters.
  8199. ( And forcing "import posix" to really "import mac" seems the only way 
  8200. to make the library sources backward compatible! ( or "path" & "macpath" )
  8201.  
  8202. Back to the question of *what* needs to be "pre-defined" - 
  8203. I'm not really sure. 
  8204.  We should start off with a minimal set of what can be easily 
  8205. determined either by the compiler or edited into a config.h file.
  8206. (1) I think (dos|posix|mac|whatever) should be #define-d in, and *not*
  8207.     determined by successful import.
  8208. (2) I think cpu-family should also be determined at compile time. 
  8209.     [ Exactly which cpu ( 80286 vs 80386, 68000 vs 68020, etc. ) 
  8210.      CAN NOT be determined by the compiler ( except for the non-downward
  8211.      compatible cases - gnu C will *only* produce 32 bit 386,486 code. )
  8212.      And I can't really thing of a use for this info except as a potential
  8213.      warning to folks stuck with only a 640K python on a PC. I expect they'll
  8214.      get SOME sort of run time message when alloc/malloc fails ]
  8215. /* the above are my "must" list. The following are increasingly maybe's */
  8216. /* although they are so easy to add that I see no reason not to.     */
  8217. (3) Endian-ness can either be determined at compile time ( by a #define )
  8218.     or perhaps better, can be a one-time low-level 'C' test that sets
  8219.     a symbol. ( look at (int) 1 as a char[2] )
  8220. (4) The sizes of native ints, longs, etc. may be useful for things like
  8221.     pack/unpack etc. It is certainly easy to figure out at compile-time.
  8222.     (  size.int = sizeof(int) ; size.long = sizeof(long) ... ) 
  8223. (5) Vaxen might need a value to indicate what type of floating point 
  8224.     number support is compiled into python. 
  8225.  
  8226.  
  8227. I propose that a dictionary object { 'cpu':'80x86', 'system':'posix', ... }
  8228. be either added to the sys module ( sys.arch, sys.machine, ? ) or that 
  8229. a new "builtin" module be added ( machine ? ) that (initially) defines 
  8230. some of these. ( We can then prototype suggested additions by importing
  8231. an additional module ( moresys.py ? ) that adds more mappings to the 
  8232. dictionary. ( And perhaps module sys should "automagically" try to 
  8233. do the equivalend of "import moresys" ? ) 
  8234.  
  8235. -------
  8236.  
  8237. Below is a module that determines what system it is by successful import.
  8238. If it is posix, it tries 'uname' or 'arch'  to get a more specific answer.
  8239. For mac & dos, it assumes the obvious for cpu. 
  8240. [ Note: there is not YET a "dosmodule" to be imported. ]
  8241.  
  8242. I will probably change the names of things, so this is not a suggested
  8243. "standard" method.
  8244. [ Who was it that said: programming is largely the art of naming things well ?
  8245.   And the proper use of namespace is really the question, if we are talking
  8246.   about defining conventions. ( I admit this is not "well-named" - just a
  8247.   prototype|exercise that suggested some of the above issues. ) ]
  8248.  
  8249.  
  8250.  
  8251. # module os
  8252. #
  8253. #   os.module = appropriate os specific module
  8254. #                 i.e. posix , or its closest functional equivalent.
  8255. #          ( mac, dos ) - there is SOME common functionality
  8256. #          between them. ( listdir(), etc. )
  8257. #   os.isposix = 0 | 1 ( if import posix was successful )
  8258. #   os.system = { 'cpu':cpu-type, os:( os-tuple ), ... }
  8259. #
  8260. #  thus:
  8261. #    import os
  8262. #    def ls( ): 
  8263. #    for file in os.module.listdir(''): print file
  8264. #  
  8265. #  SHOULD work on any system.
  8266. #  ( If we add a directory-specifier argument to the above, we either
  8267. #    need to require that the user and any-other calling routines 
  8268. #    use the proper local pathname format, or we need a posix-to-local
  8269. #    pathname converter function. ) 
  8270.  
  8271. import string
  8272.  
  8273. #
  8274. # Order of tests is because I was writing/testing on SunOS, 
  8275. # and I wanted to make sure that the other paths were tried.
  8276. #
  8277. def _sysmodule():
  8278.    system = None
  8279.    try:
  8280.      import mac
  8281.      system = mac
  8282.    except ImportError:
  8283.      try:
  8284.        import dos
  8285.        system = dos
  8286.      except ImportError:
  8287.        try:
  8288.          import posix
  8289.          system = posix
  8290.    return system
  8291.  
  8292. module = _sysmodule()
  8293.  
  8294.  
  8295. #
  8296. # Note: this version makes several calls to uname ( if posix ) with
  8297. # switches ( -m(achine) -s(ystem) -r(release) ) to get specific info.
  8298. # I didn't know whether I could rely on a posix defined order with
  8299. # the -a(ll) switch.
  8300. #   SparcStation 'uname -a' returned:    "SunOS aemsun.me 4.1.1 1 sun4c"
  8301. #   IBM RS6000 'uname -a' returned:     "AIX galen 1 3 000019501100"
  8302. # I don't know how useful cpu = "000019501100" is ! 
  8303. # Also SunOS and AIX seem to differ on the meaning of the terms
  8304. #   'version' and 'release'. AIX appears to do it correctly, returning
  8305. #  "3" for 'uname -v', SunOS returns '1' .  I was writing/testing this
  8306. #  on a SUN, so I use 'uname -r' to get " 4.1.1" 
  8307. # ( This was probably the source of my doubts about the posix order. )
  8308.  
  8309. def _cpu():
  8310.   if `module` == '<module \'dos\'>' : return 'i8x86'
  8311.   if `module` == '<module \'mac\'>' : return 'm68x00'
  8312.   if `module` == '<module \'posix\'>' :
  8313.      for cmd in [ 'uname -m', 'mach', 'arch' ]:   # add any other likely cmds!
  8314.     mach = module.popen( cmd, 'r' ).readline()
  8315.         if mach != '' : return string.strip( mach )
  8316.   return None
  8317.  
  8318.  
  8319. def _sys_generic():
  8320.     name = string.splitfields( `module`, '\'' )[1]
  8321.     return name
  8322.  
  8323. _true = 1
  8324. _false = 0
  8325.  
  8326. if _sys_generic() == 'posix' : 
  8327.    isposix = _true
  8328. else: 
  8329.    isposix = _false
  8330.  
  8331. if _sys_generic() == 'mac' : 
  8332.    ismac = _true
  8333. else:
  8334.    ismac = _false
  8335.  
  8336. if _sys_generic() == 'dos' :
  8337.    isdos = _true
  8338. else: 
  8339.    isdos = _false
  8340.  
  8341.  
  8342. def _system( ):
  8343.   generic = _sys_generic()
  8344.   if generic == 'posix' : 
  8345.       sys = module.popen( 'uname -s','r').readline()    # system
  8346.       ver = module.popen( 'uname -r','r').readline()    # release
  8347.       return ( generic, string.strip(sys), string.strip(ver) )
  8348.   else: return ( generic, )  # may want to add more specific code, e.g.
  8349.                  # ( 'ms-dos', '5.01' ), ( 'mac','7.0.1'), etc.
  8350.  
  8351.  
  8352.  
  8353. system = { 'cpu':_cpu(), 'os':_system()   }
  8354.  
  8355. def check():
  8356.   print 'module: ', `module` 
  8357.   print 'system',`system`,':'
  8358.   for each in system.keys() : 
  8359.     print (' '*4)+string.ljust(each,9), ': ', system[each]
  8360.   import os    # interesting need to import own module!
  8361.           # else: dir() => 'each' & dir(os) => error 
  8362.   for each in dir(os): 
  8363.         thing = eval('os.'+each)
  8364.     print string.ljust(each,12), string.ljust(`type(thing)`,20), thing
  8365.  
  8366.  
  8367. check()
  8368.  
  8369. #
  8370. # needed:
  8371. #  machine support: 
  8372. #   byte_order
  8373. #   sizes of native int,long 
  8374. #   ieee, vax, or other floating-point
  8375. #   ... (?)
  8376. #  os-support:
  8377. #    ??? 
  8378. #  'portable' import:
  8379. #     if os.isdos :
  8380. #    import dospath
  8381. #    path = dospath
  8382. #     if os.ismac :
  8383. #    import macpath
  8384. #    path = macpath
  8385. #     if os.isposix :
  8386. #    import posixpath
  8387. #    path = posixpath
  8388. #    etc... 
  8389. #   Note: if the above is in module 'path', then importing path
  8390. #   will make the function names 'path.path.function()' instead 
  8391. #   of 'path.function'. Probably: "from path import path" will work.
  8392. #  
  8393.  
  8394. I'll try to cobble out a prototype of a C include file for a 
  8395. prototype sys.arch when I have more time. Any other suggestions 
  8396. on what should be included/excluded ? I thing sys.arch could be 
  8397. an dictionary object like 'system' above. 
  8398.  
  8399. Note:
  8400.    I hate having to use literals for this sort of thing:
  8401.      if `module` == '<module \'dos\'>' : return 'i8x86'
  8402.      if `module` == '<module \'mac\'>' : return 'm68x00'
  8403.      if `module` == '<module \'posix\'>' :
  8404.   It is too error prone: especially when there are escapes, as above,
  8405.   it is easy to mispell and hard to check by any way other that 
  8406.   actually executing the code. This was my same complaint previously
  8407.   about exceptions. ( I know Guido agrees that the exception namespace
  8408.   needs to be finer grained - I saw it on his "to-do" list! ). If one
  8409.   wants to check on a more specific error by the additional argument, 
  8410.   one has to actually cause the exception to find out what the exact
  8411.   message is! type(''),type([]),etc. work ok as a replacement for 
  8412.   type literals ( type( open('/dev/null','r' )  ) is not as neat, but
  8413.   I haven't been forced to use it yet! :-). But I have no better 
  8414.   general solution to recommend. 
  8415.  
  8416.  
  8417.  
  8418. Another Note: 
  8419.  
  8420. * In ICON: *
  8421.  
  8422. procedure main( args )
  8423.   local n
  8424.   local c
  8425.   c :=  &digits ++ &lcase ++ &ucase 
  8426.  
  8427.   write( "Hello World!  ", &dateline )
  8428.   write( "This is " ,  &version )
  8429.   write( "&Features include:" )  
  8430.   every write( "    <*" , &features, "*>" )
  8431.  
  8432.   ... 
  8433.  
  8434.   }
  8435. end
  8436.  
  8437.  
  8438. *** produces: ***
  8439.  
  8440. Hello World!  Tuesday, February 4, 1992  12:06 pm
  8441. This is Icon Version 8.0.  May 7, 1990
  8442. &Features include:
  8443.     <*UNIX*>
  8444.     <*ASCII*>
  8445.     <*co-expressions*>
  8446.     <*direct execution*>
  8447.     <*environment variables*>
  8448.     <*error trace back*>
  8449.     <*executable images*>
  8450.     <*expandable regions*>
  8451.     <*external functions*>
  8452.     <*large integers*>
  8453.     <*math functions*>
  8454.     <*memory monitoring*>
  8455.     <*pipes*>
  8456.     <*string invocation*>
  8457.     <*system function*>
  8458.  
  8459.  
  8460. I think this is overkill. ( for python, at least. ) This was due,
  8461. I imagine, to the Icon project wanting to provide *an* implementation,
  8462. even if limited, on many platforms. I would not expect the same sort
  8463. of functional differences in python on different platforms. Here the
  8464. problem is NOT whether or not there *are*, for example, pipes, but 
  8465. how to make programs that use posix.listdir use mac.listdir with a 
  8466. minimum of effort or source convention. [ Initially, I was going to
  8467. use posix.popen & msdos.popen as an example, but I agree with the 
  8468. argument in comp.lang.icon that they are *not* semantically equal. 
  8469. BUT close enough for the equivalence to be useful. ]
  8470.  
  8471.  
  8472. I also note that there has been some discussion in comp.lang.tcl 
  8473. about handling UNIX-ism's on Mac-tcl and the converse. Mostly on 
  8474. the subject of path-name-translation ( automatic or not, etc. )
  8475. I have no opinion (yet) on the matter. 
  8476.  
  8477. Wishing-I-had-something-clever-to-say-here-like-tim-ingly yours,
  8478. ( But the above has exhausted me! As Tim once said to me: 
  8479.   "If I had more time, I would have made it shorter!" ) 
  8480.  
  8481.  - Steve
  8482.  
  8483. But to condense & reiterate the major point of the above discussion:
  8484. ( That's easier than editing & rewriting the whole thing to make it
  8485.  clearer. :-& <grrr> ) 
  8486.  
  8487. Using the success/failure of modules posix|mac|dos|{whatever} to 
  8488. determine what machine & system the program is executing on 
  8489. ( which is needed to do un-portable things portably ) is not
  8490. a good idea because it will conflict with 'aliasing' the modules
  8491. to provide portability. ( for example: making a posix.py module 
  8492. for mac that attempts to provide some posix compatible emulation. )
  8493.  
  8494.  
  8495. 
  8496. 
  8497. Received: by charon.cwi.nl with SMTP; Wed, 5 Feb 1992 00:14:20 +0100
  8498. Received: by voorn.cwi.nl with SMTP; Tue, 4 Feb 1992 23:14:19 GMT
  8499. Message-Id: <9202042314.AA15904@voorn.cwi.nl>
  8500. To: python-list@cwi.nl
  8501. Subject: Re: Python portability planning ( and prototypes )
  8502. In-Reply-To: Your message of "Tue, 04 Feb 1992 15:56:25 MET."
  8503.              <9202042056.AA05826@aemsun.med.Virginia.EDU> 
  8504. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  8505. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  8506. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  8507. Date: Wed, 05 Feb 1992 00:14:18 +0100
  8508. Sender: Guido.van.Rossum@cwi.nl
  8509.  
  8510. Steve makes some good points again!  I'll think about it more in
  8511. three weeks, when I foresee I'll have some spare time again; for now
  8512. I'll just respond to his "management summary" at the end.
  8513.  
  8514. >Using the success/failure of modules posix|mac|dos|{whatever} to 
  8515. >determine what machine & system the program is executing on 
  8516. >( which is needed to do un-portable things portably ) is not
  8517. >a good idea because it will conflict with 'aliasing' the modules
  8518. >to provide portability. ( for example: making a posix.py module 
  8519. >for mac that attempts to provide some posix compatible emulation. )
  8520.  
  8521. This depends on what you want to do.  If the attempt at posix
  8522. compatible emulation is good enough it doesn't matter whether
  8523. os.system == 'posix' or not, as long as I can say posix.stat(filename).
  8524.  
  8525. I don't see much use for knowing the CPU type, byte order, word size
  8526. and floating point format unless pack/unpack get implemented.  Come
  8527. on, most C code doesn't know or care about these (surely 99% of my own
  8528. code doesn't make any assumptions beyond what ANSI C guarantees), so
  8529. why should Python programs care?
  8530.  
  8531. There's one problem, however, which I can't completely "abstract
  8532. away": filename syntax.  I've attempted to isolate all knowledge about
  8533. pathnames in module path (or macpath on the Mac) but this is
  8534. incomplete, e.g., the released version has no predicate to test for
  8535. absobute pathnames, path.split() leaves a trailing '/' on the
  8536. directory name, there should be functions to normalize pathnames in
  8537. several ways (e.g., unify case, remove ignored characters, truncate
  8538. long names, remove "../", and expand ~ and ~user).  I am slowly adding
  8539. such functionality as the need arises, but I can't predict what
  8540. problem shows up on the next system I port it to (VMS comes to mind as
  8541. a good test case :-).
  8542.  
  8543. I admit that I've never really sat down and thought about an interface
  8544. to do every conceivable operation on pathnames.  Maybe there aren't
  8545. that many that are actually used, and we can come up with an interface
  8546. that will survive into the next century -- I don't know.
  8547.  
  8548. I do feel that with something like module path, Python has an
  8549. advantage over Tcl: there the Mac port was obviously an afterthought,
  8550. and apparently UNIX pathnames are used throughout Tcl's implementation
  8551. or in its standard library interfaces in a way that they aren't in
  8552. Python -- or why else would they even consider doing automatic
  8553. translation to Mac pathnames?  (OK, I didn't follow that discussion.)
  8554.  
  8555. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  8556. "Time is an illusion.  System time doubly so."
  8557.  
  8558. 
  8559. 
  8560. Received: by charon.cwi.nl with SMTP; Wed, 5 Feb 1992 11:02:22 +0100
  8561. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  8562.     (5.61/UUNET-internet-primary) id AA18778; Wed, 5 Feb 92 05:02:17 -0500
  8563. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  8564.     (queueing-rmail) id 050157.4264; Wed, 5 Feb 1992 05:01:57 EST
  8565. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  8566.     id AA04410; Wed, 5 Feb 92 04:48:41 EST
  8567. Received: by kaos.ksr.com (4.0/SMI-3.2)
  8568.     id AA14161; Wed, 5 Feb 92 04:48:39 EST
  8569. Message-Id: <9202050948.AA14161@kaos.ksr.com>
  8570. To: python-list@cwi.nl
  8571. Subject: More Emacs Python mode:  support for continuation lines
  8572. Date: Wed, 05 Feb 92 04:48:38 EST
  8573. From: Tim Peters <tim@ksr.com>
  8574.  
  8575. The default pattern is like:
  8576.  
  8577. if a_really_long_name == an_extraordinarily_long_name and \
  8578.       x < y < z < a < b < c < d < e < f < g < h < i and \
  8579.       not len(sys.argv) == 3:
  8580.     do_some_stuff()
  8581.     etc()
  8582.  
  8583. Change vrbl py-continuation-offset in your .emacs to something you like
  8584. better (it defaults to 2; it's added to py-indent-offset to determine
  8585. the indentation of the 1st continuation line).
  8586.  
  8587. Be warned that there *isn't* a single value you'll like in all
  8588. circumstances.  E.g., looking at the lib/*.py code, some multi-line
  8589. statements of Guido's used (in effect) a value of 8 for
  8590. py-continuation-offset, others 0, others 1, and there was even a -7 or
  8591. two (among others ...).  It depends on what you're doing & on what looks
  8592. nice in context, things I won't pretend to be able to guess.
  8593.  
  8594. For that reason, you should view the indentation Python-mode supplies
  8595. for a continuation line as just a lazy (humble <grin>) default; change
  8596. it to something you like better, and the code will follow your lead
  8597. until the statement ends.
  8598.  
  8599. Whatever oddball indentation you use during a multi-line statement will
  8600. *not* affect the normal indentation of subsequent statements.  So, e.g.,
  8601.  
  8602. animal_list = [ 'horse',    \
  8603.         'dog',        \    user hand-fiddled the indent here
  8604.         'cat',        \    LFD automatically did it here
  8605.         'python', ]        and here
  8606. death = taxes            and LFD knew that 0 indent is proper here
  8607.  
  8608. not-smart-but-accommodating-ly y'rs  - tim
  8609.  
  8610. Tim Peters   Kendall Square Research Corp
  8611. tim@ksr.com,         ksr!tim@uunet.uu.net
  8612.  
  8613. ps:  won't have time to hack Emacs the rest of the week, else I would
  8614.      have held back on this.  Back to the shadows ...
  8615.  
  8616. ;; Major mode for editing Python programs.
  8617. ;; by: Michael A. Guravage and Guido van Rossum <guido@cwi.nl>
  8618. ;;
  8619. ;; The following statements, placed in your .emacs file or site-init.el,
  8620. ;; will cause this file to be autoloaded, and python-mode invoked, when
  8621. ;; visiting .py files (assuming the file is in your load-path):
  8622. ;;
  8623. ;;    (autoload 'python-mode "python-mode" "" t)
  8624. ;;    (setq auto-mode-alist
  8625. ;;          (cons '("\\.py$" . python-mode) auto-mode-alist))
  8626.  
  8627. ;; Change log:
  8628. ;
  8629. ; Sun Feb  2 02:08:47 1992  tim
  8630. ; added support for user-defined indentation increment:
  8631. ;     added python-indent variable
  8632. ;    changed py-indent-line to indent python-indent columns
  8633. ;    fixed small bug in py-indent-line (changed start of r.e.
  8634. ;        from [^#] to [^#\n])
  8635. ;    added py-delete-char function; bound to \177
  8636. ;    changed mode blurb accordingly
  8637. ;
  8638. ; Sun Feb  2 21:48:59 1992  tim
  8639. ; renamed 'python-indent' to 'py-indent-offset' for internal consistency
  8640. ; replaced regexp in py-indent-line so it no longer indents after
  8641. ;    lines like:
  8642. ;    a = b   # ok: # found
  8643. ;    a = ': #'
  8644. ;
  8645. ; Mon Feb  3 20:37:27 1992  tim
  8646. ; renamed file to 'python-mode.el' for consistency with other Emacs
  8647. ;    language modes; changed autoload instructions accordingly
  8648. ; improved accuracy of new/changed docs
  8649. ; changed py-python-command from defconst to defvar so .emacs can
  8650. ;    override it if desired
  8651. ; add warning about indent-region; suggest indent-rigidly
  8652. ;
  8653. ; Wed Feb  5 03:23:31 1992  tim
  8654. ; added support for auto-indenting of continuation lines:
  8655. ;    new vrbl py-continuation-offset
  8656. ;    new function py-continuation-line-p
  8657. ;    new function py-goto-initial-line
  8658. ;    new function py-compute-indentation
  8659. ;    rewrote py-indent-line
  8660. ; changed py-indent-line to refrain from modifying the buffer if the
  8661. ;    indentation is already correct
  8662. ; hid the hairy colon-line regexp in a const
  8663. ; changed indent-region example to use legal Python
  8664. ; documented all that
  8665.  
  8666. (provide 'python)
  8667.  
  8668. ;; Constants and variables
  8669.  
  8670. (defvar py-python-command "python"
  8671.   "*UNIX shell command used to start Python interpreter")
  8672.  
  8673. (defvar py-indent-offset 4
  8674.   "*Indentation increment in Python mode")
  8675.  
  8676. (defvar py-continuation-offset 2
  8677.   "*Indentation (in addition to py-indent-offset) for continued lines")
  8678.  
  8679. (defvar py-mode-map nil
  8680.   "Keymap used in Python mode buffers")
  8681.  
  8682. (defvar py-mode-syntax-table nil
  8683.   "Python mode syntax table")
  8684.  
  8685. (defconst py-colon-line-re
  8686.   "\\([^#'\n]\\|'\\([^'\n\\]\\|\\\\.\\)*'\\)*:[ \t]*\\(#.*\\)?$"
  8687.   "regexp matching lines opening a new block")
  8688.  
  8689.  
  8690. ;; Initialize the keymap if it doesn't already exist
  8691.  
  8692. (if (null py-mode-map)
  8693.     (progn
  8694.       (setq py-mode-map (make-sparse-keymap))
  8695.       (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
  8696.       (define-key py-mode-map "\C-c|"     'py-execute-region)
  8697.       (define-key py-mode-map "\C-c!"     'py-shell)
  8698.       (define-key py-mode-map "\177"     'py-delete-char)))
  8699.  
  8700. ;; General Functions
  8701.  
  8702. (defun python-mode nil
  8703.   "Major mode for editing Python files.
  8704.  
  8705. \\[py-execute-buffer] sends the entire buffer to the Python interpreter.
  8706. \\[py-execute-region] sends the current region.
  8707. \\[py-shell] starts a Python interpreter window; this will be used by
  8708. subsequent \\[py-execute-buffer] or \\[py-execute-region] commands.
  8709.  
  8710.  
  8711. Variable py-indent-offset is the indentation increment.
  8712.  
  8713. \\[newline-and-indent] indents by an extra py-indent-offset columns where
  8714. necessary.  Use \\[py-delete-char] to reduce indentation.
  8715.  
  8716. \\[py-delete-char] reduces the indentation of a line by py-indent-offset columns
  8717. if point is at the first non-blank character (if any) of a line, or at
  8718. the last character of an entirely blank line; else it deletes the
  8719. preceding character, converting tabs to spaces as needed so that only
  8720. one character position is deleted.
  8721.  
  8722. Variable py-continuation-offset is the additional indentation given to
  8723. the first continuation line in a multi-line statement.  Each subsequent
  8724. continuation line in the statement inherits its indentation from the
  8725. line that precedes it, so if you don't like the default indentation
  8726. given to the first continuation line, change it to something you do like
  8727. and Python-mode will automatically use that for the remaining
  8728. continuation lines (or, until you change the indentation again).
  8729.  
  8730. Warning:  indent-region should not be used!  Use indent-rigidly to shift
  8731. regions of Python code instead.  indent-region can't guess the
  8732. indentation you had in mind, and will, e.g., change
  8733.  
  8734.     xxx
  8735.     if a < b:
  8736.         c = d
  8737.     if e < f: print 'ouch!'
  8738.  
  8739. to
  8740.     xxx
  8741.     if a < b:
  8742.         c = d
  8743.         if e < f: print 'ouch!'
  8744.  
  8745. Paragraphs are separated by blank lines only.
  8746. \\{py-mode-map}
  8747. \\[python-mode] calls the value of the variable py-mode-hook with no args,
  8748. if that value is non-nil."
  8749.   (interactive)
  8750.   (kill-all-local-variables)
  8751.   (use-local-map py-mode-map)
  8752.   (setq major-mode 'python-mode)
  8753.   (setq mode-name "Python")
  8754.   (if py-mode-syntax-table
  8755.       (set-syntax-table py-mode-syntax-table)
  8756.     (progn
  8757.       (setq py-mode-syntax-table (make-syntax-table))
  8758.       (set-syntax-table py-mode-syntax-table)
  8759.       (modify-syntax-entry ?\( "()")
  8760.       (modify-syntax-entry ?\) ")(")
  8761.       (modify-syntax-entry ?\[ "(]")
  8762.       (modify-syntax-entry ?\] ")[")
  8763.       (modify-syntax-entry ?\{ "(}")
  8764.       (modify-syntax-entry ?\} "){")
  8765.       (modify-syntax-entry ?\_ "w")
  8766.       (modify-syntax-entry ?\' "\"")    ; single quote is string quote
  8767.       (modify-syntax-entry ?\` "$")    ; backquote is open and close paren
  8768.       (modify-syntax-entry ?\# "<")    ; hash starts comment
  8769.       (modify-syntax-entry ?\n ">")    ; newline ends comment
  8770.       ))
  8771.   (make-local-variable 'paragraph-separate)
  8772.   (setq paragraph-separate "^[ \t\f]*$")
  8773.   (make-local-variable 'paragraph-start)
  8774.   (setq paragraph-start "^[ \t\f]*$")
  8775.   (make-local-variable 'require-final-newline)
  8776.   (setq require-final-newline t)
  8777.   (make-local-variable 'comment-start)
  8778.   (setq comment-start "# ")
  8779.   (make-local-variable 'comment-start-skip)
  8780.   (setq comment-start-skip "# *")
  8781.   (make-local-variable 'comment-column)
  8782.   (setq comment-column 40)
  8783.   (make-local-variable 'indent-line-function)
  8784.   (setq indent-line-function 'py-indent-line)
  8785.   (run-hooks 'py-mode-hook))
  8786.  
  8787.  
  8788. ;; Functions that execute Python commands in a subprocess
  8789.  
  8790. (defun py-shell nil
  8791.   "Start an interactive Python interpreter in another window.
  8792. The variable py-python-command names the interpreter."
  8793.   (interactive)
  8794.   (require 'shell)
  8795.   (switch-to-buffer-other-window
  8796.    (make-shell "Python" py-python-command))
  8797.   (make-local-variable 'shell-prompt-pattern)
  8798.   (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. "))
  8799.  
  8800. (defun py-execute-region (start end)
  8801.   "Send the region between START and END to a Python interpreter.
  8802. If there is a *Python* process it is used."
  8803.   (interactive "r")
  8804.   (condition-case nil
  8805.       (process-send-string "Python" (buffer-substring start end))
  8806.     (error (shell-command-on-region start end py-python-command nil))))
  8807.  
  8808. (defun py-execute-buffer nil
  8809.   "Send the contents of the buffer to a Python interpreter.
  8810. If there is a *Python* process buffer it is used."
  8811.   (interactive)
  8812.   (py-execute-region (point-min) (point-max)))
  8813.  
  8814.  
  8815. ;; Functions for Python style indentation
  8816.  
  8817. (defun py-delete-char ()
  8818.   "If at first non-blank character, or at last character of blank line,
  8819. reduce indentation by the value of variable py-indent-offset.  Else
  8820. delete preceding character, converting tabs to spaces."
  8821.   (interactive)
  8822.   (backward-delete-char-untabify
  8823.    (if (and
  8824.     (= (current-indentation) (current-column))
  8825.     (>= (current-indentation) py-indent-offset))
  8826.        py-indent-offset
  8827.      1)))
  8828.  
  8829. (defun py-indent-line ()
  8830.   "Fix the indentation of the current line according to Python rules."
  8831.   (interactive)
  8832.   (let ( (need (py-compute-indentation)) )
  8833.     (if (= (current-indentation) need)
  8834.     nil
  8835.       (beginning-of-line)
  8836.       (delete-horizontal-space)
  8837.       (indent-to need))))
  8838.  
  8839. ; go to start of current statement; usually this is the line we're on,
  8840. ; but if we're on the 2nd or following lines of a continuation block, we
  8841. ; need to go up to the first line of the block
  8842. (defun py-goto-initial-line ()
  8843.   (if (re-search-backward "[^\\]\n" (point-min) 1)
  8844.       (forward-char 2))) ; else leave point at start of restriction
  8845.  
  8846. ; t iff on continuation line == preceding line ends with backslash
  8847. (defun py-continuation-line-p ()
  8848.   (save-excursion
  8849.     (beginning-of-line)
  8850.     (if (< (- (point) 2) (point-min))
  8851.     nil
  8852.       (backward-char 2)
  8853.       (looking-at "\\\\\n"))))
  8854.  
  8855. (defun py-compute-indentation ()
  8856.   (save-excursion
  8857.     (beginning-of-line)
  8858.     (cond
  8859.      ;; are we on a continuation line?
  8860.      ( (py-continuation-line-p)
  8861.        (forward-line -1)
  8862.        (if (py-continuation-line-p) ; on at least 3rd line in block
  8863.        (current-indentation)    ; so just continue the pattern
  8864.      ;; else on 2nd line in block, so indent more
  8865.      (+ (current-indentation) py-indent-offset
  8866.         py-continuation-offset)))
  8867.      ;; not on a continuation line
  8868.  
  8869.      ;; if at start of restriction, assume they intended whatever's there
  8870.      ( (bobp) (current-indentation) )
  8871.  
  8872.      ;; else indentation based on that of the statement that precedes
  8873.      ;; us; use the first line of that statement to establish the base,
  8874.      ;; in case the user forced a non-std indentation for the
  8875.      ;; continuation lines (if any)
  8876.      ( t
  8877.        (backward-to-indentation 1)
  8878.        (let ( (need-more-p (looking-at py-colon-line-re)) )
  8879.      (py-goto-initial-line)
  8880.      (if need-more-p
  8881.          (+ (current-indentation) py-indent-offset)
  8882.        (current-indentation)))))))
  8883.  
  8884. ;; To do:
  8885. ;; - add a newline when executing buffer ending in partial line
  8886. ;; - suppress prompts when executing regions
  8887. ;; - switch back to previous buffer when starting shell
  8888. ;; - support for ptags
  8889.  
  8890. >>> END OF MSG
  8891. 
  8892. 
  8893. Replied: Thu, 06 Feb 1992 11:47:35 +0100
  8894. Replied: "python-list@cwi.nl "
  8895. Received: by charon.cwi.nl with SMTP; Wed, 5 Feb 1992 21:25:06 +0100
  8896. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa14921;
  8897.           5 Feb 92 15:24 EST
  8898. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  8899.     id AA07952; Wed, 5 Feb 92 15:15:08 EST
  8900. Date: Wed, 5 Feb 92 15:15:08 EST
  8901. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  8902. Message-Id: <9202052015.AA07952@aemsun.med.Virginia.EDU>
  8903. To: Guido.van.Rossum@cwi.nl
  8904. Subject: Re: Python portability planning ( and prototypes )
  8905. Cc: python-list@cwi.nl
  8906.  
  8907.  
  8908. me> Using the success/failure of modules posix|mac|dos|{whatever} to 
  8909. me> determine what machine & system the program is executing on 
  8910. me> ( which is needed to do un-portable things portably ) is not
  8911. me> a good idea because it will conflict with 'aliasing' the modules
  8912. me> to provide portability. ( for example: making a posix.py module 
  8913. me> for mac that attempts to provide some posix compatible emulation. )
  8914.  
  8915. guido> This depends on what you want to do.  If the attempt at posix
  8916. guido> compatible emulation is good enough it doesn't matter whether
  8917. guido> os.system == 'posix' or not, as long as I can say posix.stat(filename).
  8918.  
  8919.  
  8920. Agreed!
  8921. But what I'm aiming for is that source code libraries be portable 
  8922. *without* renaming or editing files. [ It is not a big bother right
  8923. now, but it will grow as the libraries grow. I'm trying to nip library
  8924. management in the bud.] So if there IS posix emulation, then either 
  8925. POSIXposix, macposix, or dosposix will be loaded. ( Not exactly accurate,
  8926. I know posix & mac are "built-in", but you get the idea. ). 
  8927.  
  8928. True: it is not a *frequently* needed feature. It will (probably) only 
  8929. be used (occasionally) when module are imported. 
  8930.  
  8931.  
  8932. guido> I don't see much use for knowing the CPU type, byte order, word size
  8933. guido> and floating point format unless pack/unpack get implemented.  Come
  8934. guido> on, most C code doesn't know or care about these (surely 99% of my own
  8935. guido> code doesn't make any assumptions beyond what ANSI C guarantees), so
  8936. guido> why should Python programs care?
  8937.  
  8938. You got it backward: If there is a low level implementation of pack/unpack,
  8939. then we *DON'T* need to know byte-order or work size. If pack/unpack and 
  8940. other network & native binary conversions are done in python-source, THEN
  8941. we DO need byte-order and word size. [ I don't really care about CPU type -
  8942. that was only because posix uname ( or arch ) seemed to be the only 
  8943. (indirect) way of inferring the byte-order & (possibly) work size. ]
  8944.  
  8945. Again, I admit: Infrequently needed. But essential for a whole class of
  8946. problems. C network programs use htons(), etc. to convert to/from native
  8947. and network byte orders. C programs are FULL of "sizeof()"'s [ The need
  8948. for which disappears in Python 98+% of the time, but again, that 1 or 2%
  8949. either shuts out a whole class of problems or it forces some one to dig
  8950. into internals ( either of Python or the machine ) to find out the answer
  8951. and HARD-WIRE it into their program - the extreme of non-portability! ]
  8952.  
  8953. I think the minimum necessary functionality and the most portable 
  8954. way of providing it is still an open question. [ And it is the question
  8955. I was trying to raise in the original pack/unpack discussion. ]
  8956.  
  8957.  
  8958. I may be more concerned about these things than some other folks:
  8959. I'm currently trying to move 10 YEARS and HUNDREDS of TAPES worth
  8960. of non portable vax vms record structured files some with VAX ( non IEEE )
  8961. floating point data into a portable network-neutral (i.e. readable by any 
  8962. machine without any intermediate conversion program) format, so I may
  8963. be a bit more obsessed about this than the average programmer.
  8964. [ Plus all of the data from PC's & Mac's that get ftp-ed to and from
  8965.  Sparc's, Vaxen & RS6000's et.al. ]
  8966.  
  8967. The ideal is to make both program and data portable. But we're not there
  8968. yet, so the next best thing is to provide a "portable" was to handle 
  8969. non-portable objects. [ ntohs(),htons(),etc. is an example: 
  8970. network-byte-order-16-bit-to-host-byte-order-short ( and vice versa )
  8971. For some machines that function is a no-op. But all portable network
  8972. programs that use binary data NEED that function to BE portable. ] 
  8973.  
  8974. [ I think that aesthetically, Guido, you want things to be *SO* 
  8975.   portable that you DON'T NEED to know WHAT machine you are running on,
  8976.   ( or what byte-order is native, etc. ) but I'm argueing that the only
  8977.   way to MAKE some things have a portable interface is for some level
  8978.   to know that information, and modify it's actions accordingly. ]
  8979.  
  8980. You might reply that binary file conversion is not a proper job for Python.
  8981. If I want to get down and twiddle bits, I should be using C! 
  8982. However one of the typical uses of Awk/Perl/et.al. ( and I would think 
  8983. Icon & Python both ) is to read files in one format and spit them out
  8984. in another format. Usually these are ascii character files, but the 
  8985. problems with binary files are not very different. 
  8986.  
  8987.  
  8988. [ BTW: A different but related problem is a "portable" 'file' program. 
  8989.   Someone was working on a Perl solution a while back. 'file' itself
  8990.   is limited: it matches fixed fields and there is a double byte order
  8991.   problem - the byte order of the file and the byte order of the 
  8992.   machine that 'file' is running on, that made it impossible to 
  8993.   make a portable version of /etc/magic' that could be shared by 
  8994.   different machines. The best solution I saw ( other that the perl 
  8995.   re-write of file ) was a version of /etc/magic that was run through
  8996.   the C pre-processor to make local variants. But any one local copy
  8997.   was not sharable. Before I ran into all these problems, I was 
  8998.   considering a crusade to convince developers of archiver,compression,
  8999.   graphics,etc. programs to provide a "magic" file and make their 
  9000.   installation scripts (if any) append their "magic" to "/etc/magic".
  9001.   I nice idea that turns out not to be as simple as it sounds. ]
  9002.  
  9003.  
  9004. [ various interesting comments by Guido on portable pathnames deleted ... ]
  9005.  
  9006. That is also something I'm only STARTING to think about!
  9007. It is a similar problem. How to make the different path name syntax
  9008. and conventions appear portable, without making some native files
  9009. un-nameable and un-reachable. 
  9010. *IF* we need a routine to convert to/from a cannonical ( probably 
  9011. posix style ) pathname to/from a local style pathname ( I'm not sure
  9012. that we do, but I think so, unless everyone remembers to only use 
  9013. the least common denominator path names, or unless that conversion 
  9014. is built into the builtin module's "open" . ) then that module has to 
  9015. have some local information! 
  9016.  
  9017. ( I haven't even read all the threads in comp.lang.{tcl,perl} about 
  9018.     handling unix-ism's - only noted that there was a discussion. 
  9019.   Maybe we need a comp.port  !!! [ for cross language/systems
  9020.   discussions of portable ( from the user  syntax/interface ) ways
  9021.   to handle non-portable features. ] )
  9022.  
  9023.  
  9024. - Steve 
  9025. 
  9026. 
  9027. Received: by charon.cwi.nl with SMTP; Thu, 6 Feb 1992 07:02:10 +0100
  9028. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  9029.     (5.61/UUNET-internet-primary) id AA17875; Thu, 6 Feb 92 01:02:06 -0500
  9030. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  9031.     (queueing-rmail) id 010154.15162; Thu, 6 Feb 1992 01:01:54 EST
  9032. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  9033.     id AA17660; Thu, 6 Feb 92 00:42:33 EST
  9034. Received: by kaos.ksr.com (4.0/SMI-3.2)
  9035.     id AA23396; Thu, 6 Feb 92 00:42:32 EST
  9036. Message-Id: <9202060542.AA23396@kaos.ksr.com>
  9037. To: python-list@cwi.nl
  9038. Subject: Will these Python Emacs modes never stop?!
  9039. Date: Thu, 06 Feb 92 00:42:30 EST
  9040. From: Tim Peters <tim@ksr.com>
  9041.  
  9042. Couldn't help myself <0.9 grin>.
  9043.  
  9044. Three new keys:
  9045.  
  9046. C-c TAB
  9047. C-c <
  9048. C-c >
  9049.  
  9050. These all have to do with shifting a region of Python code.  C-c TAB is
  9051. the most important one, and indeed can do anything the other two can do
  9052. (although the other two may be more convenient at times).
  9053.  
  9054. Example:  Say you're typing away in file a.py:
  9055.  
  9056.     this = that
  9057.  
  9058. and need some code to get a temp file name.  You remember you have some
  9059. in file b.py, so you visit that, cut it out, go back to a.py, and yank
  9060. it in (C-y) after the line above.  This leaves you with:
  9061.  
  9062.     this = that
  9063. if tmpfilename == '':
  9064.     for i in range(100):
  9065.     tmpfilename = '/tmp/tmp' + `posix.getpid()` + '.' + `i`
  9066.     if not path.exists(tmpfilename): break
  9067.     else: raise TmpError, 'all /tmp names already taken'
  9068.  
  9069. and an indentation headache.  No more!  Just hit C-c TAB to get:
  9070.  
  9071.     this = that
  9072.     if tmpfilename == '':
  9073.         for i in range(100):
  9074.         tmpfilename = '/tmp/tmp' + `posix.getpid()` + '.' + `i`
  9075.         if not path.exists(tmpfilename): break
  9076.         else: raise TmpError, 'all /tmp names already taken'
  9077.  
  9078. In other words, C-c TAB shifts regions of Python code, by an amount
  9079. based on context, much as TAB does for individual lines.
  9080.  
  9081. You can also give an absolute shift count to C-c TAB.  C-c < and C-c >
  9082. just shift a region of code left or right (respectively) by py-indent-
  9083. offset columns (or by any other amount you like, if you give them a
  9084. numeric prefix argument).
  9085.  
  9086. getting-close-to-having-a-real-language-mode<grin>-ly y'rs  - tim
  9087.  
  9088. Tim Peters   Kendall Square Research Corp
  9089. tim@ksr.com,         ksr!tim@uunet.uu.net
  9090.  
  9091. ;; Major mode for editing Python programs.
  9092. ;; by: Michael A. Guravage and Guido van Rossum <guido@cwi.nl>
  9093. ;;
  9094. ;; The following statements, placed in your .emacs file or site-init.el,
  9095. ;; will cause this file to be autoloaded, and python-mode invoked, when
  9096. ;; visiting .py files (assuming the file is in your load-path):
  9097. ;;
  9098. ;;    (autoload 'python-mode "python-mode" "" t)
  9099. ;;    (setq auto-mode-alist
  9100. ;;          (cons '("\\.py$" . python-mode) auto-mode-alist))
  9101.  
  9102. ;; Change log:
  9103. ;
  9104. ; Sun Feb  2 02:08:47 1992  tim
  9105. ; added support for user-defined indentation increment:
  9106. ;     added python-indent variable
  9107. ;    changed py-indent-line to indent python-indent columns
  9108. ;    fixed small bug in py-indent-line (changed start of r.e.
  9109. ;        from [^#] to [^#\n])
  9110. ;    added py-delete-char function; bound to \177
  9111. ;    changed mode blurb accordingly
  9112. ;
  9113. ; Sun Feb  2 21:48:59 1992  tim
  9114. ; renamed 'python-indent' to 'py-indent-offset' for internal consistency
  9115. ; replaced regexp in py-indent-line so it no longer indents after
  9116. ;    lines like:
  9117. ;    a = b   # ok: # found
  9118. ;    a = ': #'
  9119. ;
  9120. ; Mon Feb  3 20:37:27 1992  tim
  9121. ; renamed file to 'python-mode.el' for consistency with other Emacs
  9122. ;    language modes; changed autoload instructions accordingly
  9123. ; improved accuracy of new/changed docs
  9124. ; changed py-python-command from defconst to defvar so .emacs can
  9125. ;    override it if desired
  9126. ; add warning about indent-region; suggest indent-rigidly
  9127. ;
  9128. ; Wed Feb  5 03:23:31 1992  tim
  9129. ; added support for auto-indenting of continuation lines:
  9130. ;    new vrbl py-continuation-offset
  9131. ;    new function py-continuation-line-p
  9132. ;    new function py-goto-initial-line
  9133. ;    new function py-compute-indentation
  9134. ;    rewrote py-indent-line
  9135. ; changed py-indent-line to refrain from modifying the buffer if the
  9136. ;    indentation is already correct
  9137. ; hid the hairy colon-line regexp in a const
  9138. ; changed indent-region example to use legal Python
  9139. ; documented all that
  9140. ;
  9141. ; Wed Feb  5 21:52:15 1992  tim
  9142. ; changed new doc strings so first line makes sense on its own
  9143. ; new function py-shift-region; bound to C-c TAB
  9144. ; new function py-shift-region-left; bound to C-c <
  9145. ; new function py-shift-region-right; bound to C-c >
  9146. ; reorganized mode blurb
  9147. ; sped up py-continuation-line-p
  9148.  
  9149. (provide 'python)
  9150.  
  9151. ;; Constants and variables
  9152.  
  9153. (defvar py-python-command "python"
  9154.   "*UNIX shell command used to start Python interpreter")
  9155.  
  9156. (defvar py-indent-offset 4
  9157.   "*Indentation increment in Python mode")
  9158.  
  9159. (defvar py-continuation-offset 2
  9160.   "*Indentation (in addition to py-indent-offset) for continued lines")
  9161.  
  9162. (defvar py-mode-map nil
  9163.   "Keymap used in Python mode buffers")
  9164.  
  9165. (defvar py-mode-syntax-table nil
  9166.   "Python mode syntax table")
  9167.  
  9168. (defconst py-colon-line-re
  9169.   "\\([^#'\n]\\|'\\([^'\n\\]\\|\\\\.\\)*'\\)*:[ \t]*\\(#.*\\)?$"
  9170.   "regexp matching lines opening a new block")
  9171.  
  9172.  
  9173. ;; Initialize the keymap if it doesn't already exist
  9174.  
  9175. (if (null py-mode-map)
  9176.     (progn
  9177.       (setq py-mode-map (make-sparse-keymap))
  9178.       (define-key py-mode-map "\C-c\C-c" 'py-execute-buffer)
  9179.       (define-key py-mode-map "\C-c|"     'py-execute-region)
  9180.       (define-key py-mode-map "\C-c!"     'py-shell)
  9181.       (define-key py-mode-map "\177"     'py-delete-char)
  9182.       (define-key py-mode-map "\C-c\C-i" 'py-shift-region)
  9183.       (define-key py-mode-map "\C-c<"     'py-shift-region-left)
  9184.       (define-key py-mode-map "\C-c>"     'py-shift-region-right)))
  9185.  
  9186. ;; General Functions
  9187.  
  9188. (defun python-mode nil
  9189.   "Major mode for editing Python files.
  9190.  
  9191. Paragraphs are separated by blank lines only.
  9192.  
  9193. \\[python-mode] calls the value of the variable py-mode-hook with no args,
  9194. if that value is non-nil.
  9195.  
  9196. INTERFACE TO PYTHON INTERPRETER
  9197.  
  9198. \\[py-execute-buffer]    sends the entire buffer to the Python interpreter
  9199. \\[py-execute-region]    sends the current region.
  9200. \\[py-shell]    starts a Python interpreter window; this will be used by
  9201.     subsequent \\[py-execute-buffer] or \\[py-execute-region] commands
  9202.  
  9203. VARIABLES
  9204.  
  9205. py-indent-offset    indentation increment
  9206. py-continuation-offset    extra indentation given to continuation lines
  9207.  
  9208. INDENTATION
  9209.  
  9210. \\[newline-and-indent]    indent line appropriately
  9211. \\[py-delete-char]    reduce indentation, or delete single character
  9212. \\[py-shift-region-left]    shift region left by py-indent-offset
  9213. \\[py-shift-region-right]    shift region right by py-indent-offset
  9214. \\[py-shift-region]    shift region to match its context
  9215.  
  9216. \\[newline-and-indent] indents by an extra py-indent-offset columns where
  9217. necessary.
  9218.  
  9219. \\[py-delete-char] reduces the indentation of a line by py-indent-offset columns
  9220. if point is at the first non-blank character (if any) of a line, or at
  9221. the last character of an entirely blank line; else it deletes the
  9222. preceding character, converting tabs to spaces as needed so that only
  9223. one character position is deleted.
  9224.  
  9225. py-continuation-offset is the additional indentation given to the first
  9226. continuation line in a multi-line statement.  Each subsequent
  9227. continuation line in the statement inherits its indentation from the
  9228. line that precedes it, so if you don't like the default indentation
  9229. given to the first continuation line, change it to something you do like
  9230. and Python-mode will automatically use that for the remaining
  9231. continuation lines (or, until you change the indentation again).
  9232.  
  9233. \\[py-shift-region-left] shifts the region left by py-indent-offset columns.
  9234.  
  9235. \\[py-shift-region-right] shifts the region right by py-indent-offset columns.
  9236.  
  9237. \\[py-shift-region] shifts the region to indent it properly with respect
  9238. to the line preceding the region.  This is useful when code blocks are
  9239. moved or yanked, or when enclosing control structures are introduced or
  9240. removed.
  9241.  
  9242. The three \"shift\" functions above also honor numeric prefix arguments;
  9243. see the individual function documentation for details.
  9244.  
  9245. Warning:  indent-region should not normally be used!  It can't guess the
  9246. indentation you had in mind, and will, e.g., change
  9247.  
  9248.     xxx
  9249.     if a < b:
  9250.         c = d
  9251.     if e < f: print 'ouch!'
  9252.  
  9253. to
  9254.     xxx
  9255.     if a < b:
  9256.         c = d
  9257.         if e < f: print 'ouch!'
  9258.  
  9259. MODE MAP
  9260. \\{py-mode-map}"
  9261.   (interactive)
  9262.   (kill-all-local-variables)
  9263.   (use-local-map py-mode-map)
  9264.   (setq major-mode 'python-mode)
  9265.   (setq mode-name "Python")
  9266.   (if py-mode-syntax-table
  9267.       (set-syntax-table py-mode-syntax-table)
  9268.     (progn
  9269.       (setq py-mode-syntax-table (make-syntax-table))
  9270.       (set-syntax-table py-mode-syntax-table)
  9271.       (modify-syntax-entry ?\( "()")
  9272.       (modify-syntax-entry ?\) ")(")
  9273.       (modify-syntax-entry ?\[ "(]")
  9274.       (modify-syntax-entry ?\] ")[")
  9275.       (modify-syntax-entry ?\{ "(}")
  9276.       (modify-syntax-entry ?\} "){")
  9277.       (modify-syntax-entry ?\_ "w")
  9278.       (modify-syntax-entry ?\' "\"")    ; single quote is string quote
  9279.       (modify-syntax-entry ?\` "$")    ; backquote is open and close paren
  9280.       (modify-syntax-entry ?\# "<")    ; hash starts comment
  9281.       (modify-syntax-entry ?\n ">")    ; newline ends comment
  9282.       ))
  9283.   (make-local-variable 'paragraph-separate)
  9284.   (setq paragraph-separate "^[ \t\f]*$")
  9285.   (make-local-variable 'paragraph-start)
  9286.   (setq paragraph-start "^[ \t\f]*$")
  9287.   (make-local-variable 'require-final-newline)
  9288.   (setq require-final-newline t)
  9289.   (make-local-variable 'comment-start)
  9290.   (setq comment-start "# ")
  9291.   (make-local-variable 'comment-start-skip)
  9292.   (setq comment-start-skip "# *")
  9293.   (make-local-variable 'comment-column)
  9294.   (setq comment-column 40)
  9295.   (make-local-variable 'indent-line-function)
  9296.   (setq indent-line-function 'py-indent-line)
  9297.   (run-hooks 'py-mode-hook))
  9298.  
  9299.  
  9300. ;; Functions that execute Python commands in a subprocess
  9301.  
  9302. (defun py-shell nil
  9303.   "Start an interactive Python interpreter in another window.
  9304. The variable py-python-command names the interpreter."
  9305.   (interactive)
  9306.   (require 'shell)
  9307.   (switch-to-buffer-other-window
  9308.    (make-shell "Python" py-python-command))
  9309.   (make-local-variable 'shell-prompt-pattern)
  9310.   (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. "))
  9311.  
  9312. (defun py-execute-region (start end)
  9313.   "Send the region between START and END to a Python interpreter.
  9314. If there is a *Python* process it is used."
  9315.   (interactive "r")
  9316.   (condition-case nil
  9317.       (process-send-string "Python" (buffer-substring start end))
  9318.     (error (shell-command-on-region start end py-python-command nil))))
  9319.  
  9320. (defun py-execute-buffer nil
  9321.   "Send the contents of the buffer to a Python interpreter.
  9322. If there is a *Python* process buffer it is used."
  9323.   (interactive)
  9324.   (py-execute-region (point-min) (point-max)))
  9325.  
  9326.  
  9327. ;; Functions for Python style indentation
  9328.  
  9329. (defun py-delete-char ()
  9330.   "Reduce indentation or delete character.
  9331. If at first non-blank character, or at last character of blank line,
  9332. reduce indentation by the value of variable py-indent-offset.  Else
  9333. delete preceding character, converting tabs to spaces."
  9334.   (interactive)
  9335.   (backward-delete-char-untabify
  9336.    (if (and
  9337.     (= (current-indentation) (current-column))
  9338.     (>= (current-indentation) py-indent-offset))
  9339.        py-indent-offset
  9340.      1)))
  9341.  
  9342. (defun py-indent-line ()
  9343.   "Fix the indentation of the current line according to Python rules."
  9344.   (interactive)
  9345.   (let ( (need (py-compute-indentation)) )
  9346.     (if (= (current-indentation) need)
  9347.     nil
  9348.       (beginning-of-line)
  9349.       (delete-horizontal-space)
  9350.       (indent-to need))))
  9351.  
  9352. ; go to start of current statement; usually this is the line we're on,
  9353. ; but if we're on the 2nd or following lines of a continuation block, we
  9354. ; need to go up to the first line of the block
  9355. (defun py-goto-initial-line ()
  9356.   (if (re-search-backward "[^\\]\n" (point-min) 1)
  9357.       (forward-char 2))) ; else leave point at start of restriction
  9358.  
  9359. ; t iff on continuation line == preceding line ends with backslash
  9360. (defun py-continuation-line-p ()
  9361.   (save-excursion
  9362.     (beginning-of-line)
  9363.     (eq (char-after (- (point) 2)) ; nil if argument out of range
  9364.     ?\\ )))
  9365.  
  9366. (defun py-compute-indentation ()
  9367.   (save-excursion
  9368.     (beginning-of-line)
  9369.     (cond
  9370.      ;; are we on a continuation line?
  9371.      ( (py-continuation-line-p)
  9372.        (forward-line -1)
  9373.        (if (py-continuation-line-p) ; on at least 3rd line in block
  9374.        (current-indentation)    ; so just continue the pattern
  9375.      ;; else on 2nd line in block, so indent more
  9376.      (+ (current-indentation) py-indent-offset
  9377.         py-continuation-offset)))
  9378.      ;; not on a continuation line
  9379.  
  9380.      ;; if at start of restriction, assume they intended whatever's there
  9381.      ( (bobp) (current-indentation) )
  9382.  
  9383.      ;; else indentation based on that of the statement that precedes
  9384.      ;; us; use the first line of that statement to establish the base,
  9385.      ;; in case the user forced a non-std indentation for the
  9386.      ;; continuation lines (if any)
  9387.      ( t
  9388.        (backward-to-indentation 1)
  9389.        (let ( (need-more-p (looking-at py-colon-line-re)) )
  9390.      (py-goto-initial-line)
  9391.      (if need-more-p
  9392.          (+ (current-indentation) py-indent-offset)
  9393.        (current-indentation)))))))
  9394.  
  9395. (defun py-shift-region (start end &optional count)
  9396.   "Shift region of Python code horizontally.
  9397. The lines from the line containing the start of the current region up
  9398. to (but not including) the line containing the end of the region are
  9399. shifted the same amount, so that they're indented properly with respect
  9400. to the line immediately preceding the region.  This is useful when code
  9401. blocks are moved or yanked, or when enclosing control structures are
  9402. introduced or removed.
  9403. If a prefix argument is given, the region is instead shifted by that
  9404. many columns (to the right if the prefix argument is positive, else to
  9405. the left)."
  9406.   (interactive "*r\nP")   ; region; raw prefix arg
  9407.   (save-excursion
  9408.     (goto-char end)   (beginning-of-line) (setq end (point))
  9409.     (goto-char start) (beginning-of-line) (setq start (point))
  9410.     (indent-rigidly start end
  9411.             (if (null count)
  9412.             (- (py-compute-indentation)
  9413.                (current-indentation))
  9414.               (prefix-numeric-value count)))))
  9415.  
  9416. (defun py-shift-region-left (start end &optional count)
  9417.   "Shift region of Python code to the left.
  9418. The lines from the line containing the start of the current region up
  9419. to (but not including) the line containing the end of the region are
  9420. shifted to the left, by py-indent-offset columns.
  9421.  
  9422. If a prefix argument is given, the region is instead shifted by that
  9423. many columns."
  9424.   (interactive "*r\nP")   ; region; raw prefix arg
  9425.   (py-shift-region start end
  9426.            (- (if (null count)
  9427.               py-indent-offset
  9428.             (prefix-numeric-value count)))))
  9429.  
  9430. (defun py-shift-region-right (start end &optional count)
  9431.   "Shift region of Python code to the right.
  9432. The lines from the line containing the start of the current region up
  9433. to (but not including) the line containing the end of the region are
  9434. shifted to the right, by py-indent-offset columns.
  9435.  
  9436. If a prefix argument is given, the region is instead shifted by that
  9437. many columns."
  9438.   (interactive "*r\nP")   ; region; raw prefix arg
  9439.   (py-shift-region start end (or count py-indent-offset)))
  9440.  
  9441. ;; To do:
  9442. ;; - add a newline when executing buffer ending in partial line
  9443. ;; - suppress prompts when executing regions
  9444. ;; - switch back to previous buffer when starting shell
  9445. ;; - support for ptags
  9446.  
  9447. >>> END OF MSG
  9448. 
  9449. 
  9450. Received: by charon.cwi.nl with SMTP; Thu, 6 Feb 1992 11:47:39 +0100
  9451. Received: by voorn.cwi.nl with SMTP; Thu, 6 Feb 1992 10:47:37 GMT
  9452. Message-Id: <9202061047.AA20370@voorn.cwi.nl>
  9453. To: python-list@cwi.nl
  9454. Subject: Re: Python portability planning ( and prototypes ) 
  9455. In-Reply-To: Your message of "Wed, 05 Feb 1992 15:15:08 MET."
  9456.              <9202052015.AA07952@aemsun.med.Virginia.EDU> 
  9457. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  9458. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  9459. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  9460. Date: Thu, 06 Feb 1992 11:47:36 +0100
  9461. Sender: Guido.van.Rossum@cwi.nl
  9462.  
  9463. >But what I'm aiming for is that source code libraries be portable
  9464.                                  ^^^^^^^^^^^^^^^^^^^^^
  9465. >*without* renaming or editing files.
  9466.  
  9467. Good point!  (I assume you mean what I call the "standard library
  9468. modules".)
  9469.  
  9470. >[ It is not a big bother right
  9471. >now, but it will grow as the libraries grow. I'm trying to nip library
  9472. >management in the bud.] So if there IS posix emulation, then either 
  9473. >POSIXposix, macposix, or dosposix will be loaded. ( Not exactly accurate,
  9474. >I know posix & mac are "built-in", but you get the idea. ). 
  9475.  
  9476. Excellent idea, this is a very nice solution.  As an extension, the
  9477. built-in posix emulations could export themselves under two names:
  9478. 'posix' for naive programs, and '<OSNAME>posix' for programs that want
  9479. to know.  (I suggest using 'unix' for <OSNAME> in the UNIX case.)
  9480.  
  9481. >guido> I don't see much use for knowing the CPU type, byte order, word size
  9482. >guido> and floating point format unless pack/unpack get implemented.  Come
  9483. >guido> on, most C code doesn't know or care about these (surely 99% of my own
  9484. >guido> code doesn't make any assumptions beyond what ANSI C guarantees), so
  9485. >guido> why should Python programs care?
  9486. >
  9487. >You got it backward: If there is a low level implementation of pack/unpack,
  9488. >then we *DON'T* need to know byte-order or work size. If pack/unpack and 
  9489. >other network & native binary conversions are done in python-source, THEN
  9490. >we DO need byte-order and word size. [ I don't really care about CPU type -
  9491. >that was only because posix uname ( or arch ) seemed to be the only 
  9492. >(indirect) way of inferring the byte-order & (possibly) work size. ]
  9493.  
  9494. I still don't agree.  You don't need the byte order and word size
  9495. of the current machine -- you need the byte order and word size of the
  9496. machine that created the binary file you are trying to read (or where
  9497. the binary file you are going to write will be used).  Since you say
  9498. you want to do this for conversion jobs, I expect these will often be
  9499. different from the current machine!
  9500.  
  9501. >Again, I admit: Infrequently needed. But essential for a whole class of
  9502. >problems. C network programs use htons(), etc. to convert to/from native
  9503. >and network byte orders. C programs are FULL of "sizeof()"'s [ The need
  9504. >for which disappears in Python 98+% of the time, but again, that 1 or 2%
  9505. >either shuts out a whole class of problems or it forces some one to dig
  9506. >into internals ( either of Python or the machine ) to find out the answer
  9507. >and HARD-WIRE it into their program - the extreme of non-portability! ]
  9508.  
  9509. It's extremely simple to add a command line option, environment
  9510. variable or more sophisticated configuration mechanism whereby byte
  9511. order and word size can be specified to such conversion programs.
  9512. Hard-wiring in the native parameters makes the program non-portable if
  9513. the conversion has to run on a different platform.  As an example, C++
  9514. to C translators usually read a little file specifying the size and
  9515. alignment of C data structures on the target machine.
  9516.  
  9517. I admit that it's probably useful to have the native parameters
  9518. available for defaults in many cases.  So they should be available,
  9519. and getting these from a built-in interface is more reliable than
  9520. using a configuration file.  The next version of the posix module will
  9521. already contain a uname() call, so it's also a possibility make a
  9522. portable library module that translates pairs of (osname, machinetype)
  9523. into byte order and int size.  (There's no rule in POSIX that says
  9524. that different OS's running on the same machine must return the same
  9525. string, so the osname must figure in the argument.)
  9526.  
  9527. >[ I think that aesthetically, Guido, you want things to be *SO* 
  9528. >  portable that you DON'T NEED to know WHAT machine you are running on,
  9529. >  ( or what byte-order is native, etc. ) but I'm argueing that the only
  9530. >  way to MAKE some things have a portable interface is for some level
  9531. >  to know that information, and modify it's actions accordingly. ]
  9532.  
  9533. Agreed.  Is what I said above enough water it the wine?
  9534.  
  9535. >You might reply that binary file conversion is not a proper job for Python.
  9536. >If I want to get down and twiddle bits, I should be using C!
  9537.  
  9538. But I won't.  Python can do this, and if reading a binary file is a
  9539. small part of the conversion process, or if it's a one-time job,
  9540. Python is quite suitable (especially if you already know to program in
  9541. Python).
  9542.  
  9543. >*IF* we need a routine to convert to/from a cannonical ( probably 
  9544. >posix style ) pathname to/from a local style pathname ( I'm not sure
  9545. >that we do, but I think so, unless everyone remembers to only use 
  9546. >the least common denominator path names, or unless that conversion 
  9547. >is built into the builtin module's "open" . ) then that module has to 
  9548. >have some local information!
  9549.  
  9550. I am very much against adopting a standard Python style of pathnames
  9551. (i.e. saying that '/' is the pathname delimiter on all systems and
  9552. translating to the native delimiter on systems where it differs).
  9553. The arguments passed to open(), stat(), listdir() etc. and the return
  9554. values of getcwd(), readlink() etc. must definitely be pathnames in
  9555. the native system's syntax.  Since most pathnames will be typed by the
  9556. user (e.g. on the command line) or retrieved from the system somehow,
  9557. this can't be a big problem.  Where you hard-code pathnames in a
  9558. program they usually have to be edited when moving the program to a
  9559. different site anyway.
  9560.  
  9561. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  9562. "I could be arguing in my spare time."
  9563. 
  9564. 
  9565. Received: by charon.cwi.nl with SMTP; Thu, 6 Feb 1992 19:41:04 +0100
  9566. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa00770;
  9567.           6 Feb 92 13:40 EST
  9568. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  9569.     id AA09247; Thu, 6 Feb 92 13:31:15 EST
  9570. Date: Thu, 6 Feb 92 13:31:15 EST
  9571. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  9572. Message-Id: <9202061831.AA09247@aemsun.med.Virginia.EDU>
  9573. To: Guido.van.Rossum@cwi.nl
  9574. Subject: Re: Python portability planning ( and prototypes )
  9575. Cc: python-list@cwi.nl
  9576.  
  9577.  
  9578. > >guido> I don't see much use for knowing the CPU type, byte order, word size
  9579. > >guido> and floating point format unless pack/unpack get implemented.  
  9580. > >
  9581. > >You got it backward: If there is a low level implementation of pack/unpack,
  9582. > >then we *DON'T* need to know byte-order or work size. If pack/unpack and 
  9583. > >other network & native binary conversions are done in python-source, THEN
  9584. > >we DO need byte-order and word size. [ I don't really care about CPU type -
  9585. > >that was only because posix uname ( or arch ) seemed to be the only 
  9586. > >(indirect) way of inferring the byte-order & (possibly) work size. ]
  9587. > I still don't agree.  You don't need the byte order and word size
  9588. > of the current machine -- you need the byte order and word size of the
  9589. > machine that created the binary file you are trying to read (or where
  9590. > the binary file you are going to write will be used).  Since you say
  9591. > you want to do this for conversion jobs, I expect these will often be
  9592. > different from the current machine!
  9593. >
  9594.  
  9595. Oops! Well, maybe what I meant was you need it *even more* if you need
  9596. to implement pack/unpack in python-source.
  9597.  
  9598.  
  9599. > It's extremely simple to add a command line option, environment
  9600. > variable or more sophisticated configuration mechanism whereby byte
  9601. > order and word size can be specified to such conversion programs.
  9602.  
  9603. If I may quote you from another context: 
  9604. guido> I don't see much use for knowing the CPU type, byte order, word size
  9605. guido> and floating point format unless pack/unpack get implemented.  Come
  9606. guido> on, most C code doesn't know or care about these  ... 
  9607.  
  9608. Oh ! So the *programmers* DON'T know what byte order they are using, but now
  9609. you expect the *USERS* to know! :-) 
  9610.  
  9611. Actually, you have a point. I don't expect them to know what byte order 
  9612. is, but it is reasonable for them to know that what they are trying to 
  9613. do is convert IBM-PC files to Sparc/Unix files of some type. So I reject
  9614. the idea of an option like 'dd's "conv=swab", but something like 
  9615. "-from pc" is reasonable. But I still want to keep the "low-level" code
  9616. portable. 
  9617.  
  9618.  
  9619. Also, I WILL admit that *I* don't know the byte order of various machines
  9620. off the top of my head. But I *do* know that there is a "network byte
  9621. order" and conversions to/from that and native byte order. ( This, again,
  9622. is my paradigmatic example of "good portable non-portabilities" ).
  9623. And I sometimes remember that two particular machines are different 
  9624. ( even if I can't remember which one is big-endian or how they relate
  9625. to a third architecture. ). [ "Oh - this file came from a PC, so I'd
  9626. better 'swab' it!" - That's the paradigmatic "bad" example! ] 
  9627.  
  9628.  
  9629.  
  9630. > >*IF* we need a routine to convert to/from a cannonical ( probably 
  9631. > >posix style ) pathname to/from a local style pathname ( I'm not sure
  9632. > >that we do, but I think so, unless everyone remembers to only use 
  9633. > >the least common denominator path names, or unless that conversion 
  9634. > >is built into the builtin module's "open" . ) then that module has to 
  9635. > >have some local information!
  9636. > I am very much against adopting a standard Python style of pathnames
  9637. > (i.e. saying that '/' is the pathname delimiter on all systems and
  9638. > translating to the native delimiter on systems where it differs).
  9639. > The arguments passed to open(), stat(), listdir() etc. and the return
  9640. > values of getcwd(), readlink() etc. must definitely be pathnames in
  9641. > the native system's syntax.  Since most pathnames will be typed by the
  9642. > user (e.g. on the command line) or retrieved from the system somehow,
  9643. > this can't be a big problem.  Where you hard-code pathnames in a
  9644. > program they usually have to be edited when moving the program to a
  9645. > different site anyway.
  9646.  
  9647. I agree. ( I think! :-)
  9648. "Native" names should be the norm for interactive usage.
  9649. Where pathname need to be written into a program, then it is not
  9650. unreasonable to expect the programmer to explicitly use a 
  9651. "path.to_native" conversion function, to indicate that he doesn't
  9652. actually MIND if the pathname get's munged up ( truncated | character
  9653. translated | etc. ) as long as there is a determinate mapping. 
  9654. ( for practical purposes one-to-one )
  9655.  
  9656.  
  9657.  
  9658. I think we are in agreement on: 
  9659. (1) There will be ( for example ) a 'path' module for unix|dos|mac|etc.
  9660.     that will attempt to hide or at least isolate machine differences.
  9661. (2) That portable code should only need to import 'path', and not 
  9662.     need to figure out which 'specific' module ( unixpath|macpath|dospath )
  9663.     it needs to load. [ I don't care what mechanics we choose to do this,
  9664.     as long as we can hide the machinery! ]
  9665. (3) But searching for module dependencies and renameing files is not
  9666.     the preferred solution to the above. [ The machinery here is 
  9667.     painfully visible, even if only visible to ONE person ( the site
  9668.     maintainer/installer of Python. ]
  9669.  
  9670. The binary read/write issue is a completely different question. The two
  9671. are only linked because of using successful import of a "built-in" module
  9672. to determine local information. [ Which is why it is a bad method. ]
  9673.  
  9674.  
  9675. Gotta go hear a man talk about Wavelet's!
  9676. Bye for now,
  9677. - Steve
  9678.  
  9679. [ BTW: I have managed to get my packet drivers working on my 486 PC,
  9680.   So I can telnet the python sources onto it. I have the Gnu C compiler
  9681.   PC port installed, So I hope to start porting Python *REAL SOON* ]
  9682. 
  9683. 
  9684. Received: by charon.cwi.nl with SMTP; Thu, 6 Feb 1992 23:21:34 +0100
  9685. Received: by voorn.cwi.nl with SMTP; Thu, 6 Feb 1992 22:21:33 GMT
  9686. Message-Id: <9202062221.AA21925@voorn.cwi.nl>
  9687. To: python-list@cwi.nl
  9688. Subject: Re: Python portability planning ( and prototypes ) 
  9689. In-Reply-To: Your message of "Thu, 06 Feb 1992 13:31:15 MET."
  9690.              <9202061831.AA09247@aemsun.med.Virginia.EDU> 
  9691. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  9692. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  9693. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  9694. Date: Thu, 06 Feb 1992 23:21:32 +0100
  9695. Sender: Guido.van.Rossum@cwi.nl
  9696.  
  9697. >Oh ! So the *programmers* DON'T know what byte order they are using, but now
  9698. >you expect the *USERS* to know! :-) 
  9699.  
  9700. Actually, for most file formats I've encountered it's pretty easy for
  9701. the conversion program to infer the byte order used by a file from the
  9702. first few bytes of data (e.g. a magic number) -- presuming the
  9703. conversion program what the data is supposed to look like...
  9704.  
  9705. In general users needn't know what byte order a file is in -- the hard
  9706. part is knowing what the format is in the first place.  Maybe "magic
  9707. (a replacement for file(1) written in Perl just posted on the net) can
  9708. help them with that -- although for me it just said
  9709. ``Undefined subroutine "main'BufMatch" called at magic.pl line 429.''
  9710.  
  9711. >Actually, you have a point. I don't expect them to know what byte order 
  9712. >is, but it is reasonable for them to know that what they are trying to 
  9713. >do is convert IBM-PC files to Sparc/Unix files of some type. So I reject
  9714. >the idea of an option like 'dd's "conv=swab", but something like 
  9715. >"-from pc" is reasonable. But I still want to keep the "low-level" code
  9716. >portable. 
  9717.  
  9718. Given some table mapping architectures to byte orders you could add a
  9719. "-to sparc" option.  But of course the "dd" example is naive: no file
  9720. formats consists of arrays of words that can be byte-swapped like
  9721. that; in reality you will have to write code that understands the data
  9722. and knows when to swap words or longs or nothing.
  9723.  
  9724. >Also, I WILL admit that *I* don't know the byte order of various machines
  9725. >off the top of my head. But I *do* know that there is a "network byte
  9726. >order" and conversions to/from that and native byte order.
  9727.  
  9728. Reading a word in network byte order is easy in Python:
  9729.  
  9730.     def rwordn(fp):
  9731.         s = fp.read(2)
  9732.         if len(s) < 2: raise EOFError
  9733.         hibyte, lobyte = ord(s[0]), ord(s[1])
  9734.         return hibyte<<8 | lobyte
  9735.  
  9736. Writing is of course as easy.  There's no need to know the byte order
  9737. used by Python here.  This assumes network byte order is big-endian
  9738. (hibyte first), which is what the Internet specifies -- but who knows
  9739. what other networks use?
  9740.  
  9741. You can write similar routines rwordb() and rwordl() to read words in
  9742. big and little endian order -- rwordb() will of course be identical to
  9743. rwordn().
  9744.  
  9745. >Where pathname need to be written into a program, then it is not
  9746. >unreasonable to expect the programmer to explicitly use a 
  9747. >"path.to_native" conversion function, to indicate that he doesn't
  9748. >actually MIND if the pathname get's munged up ( truncated | character
  9749. >translated | etc. ) as long as there is a determinate mapping. 
  9750. >( for practical purposes one-to-one )
  9751.  
  9752. I don't understand this.  Pathnames hardcoded into programs are almost
  9753. always things like /etc/termcap, /usr/tmp or the equivalent of
  9754. $HOME/.mh_profile.  How would you expect a path.to_native for Mac or
  9755. MS-DOS to translate these?  You can *never* expect to be able to move
  9756. a program containing hardcoded pathnames to such a system without
  9757. having to edit them.
  9758.  
  9759. Filenames (i.e. no slash on UNIX) are a different matter, but even
  9760. there the choice of names usually has to be revised by a human being
  9761. when moving to a different O.S., e.g. names begginning with a . are
  9762. not usable on MS-DOS.
  9763.  
  9764. >I think we are in agreement on: 
  9765. >(1) There will be ( for example ) a 'path' module for unix|dos|mac|etc.
  9766. >    that will attempt to hide or at least isolate machine differences.
  9767. >(2) That portable code should only need to import 'path', and not 
  9768. >    need to figure out which 'specific' module ( unixpath|macpath|dospath )
  9769. >    it needs to load. [ I don't care what mechanics we choose to do this,
  9770. >    as long as we can hide the machinery! ]
  9771. >(3) But searching for module dependencies and renameing files is not
  9772. >    the preferred solution to the above. [ The machinery here is 
  9773. >    painfully visible, even if only visible to ONE person ( the site
  9774. >    maintainer/installer of Python. ]
  9775.  
  9776. Yes.  Yes!  YES!!!
  9777.  
  9778. I will try to put a lot of this the a next release, or at least make a
  9779. decent attempt (after all I routinely move Python code between a Mac
  9780. and UNIX so I have ample opportunity to test it in two totally
  9781. different environments).
  9782.  
  9783. >[ BTW: I have managed to get my packet drivers working on my 486 PC,
  9784. >  So I can telnet the python sources onto it. I have the Gnu C compiler
  9785. >  PC port installed, So I hope to start porting Python *REAL SOON* ]
  9786.  
  9787. That's good news!
  9788.  
  9789. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  9790. "This is an ex-parrot"
  9791. 
  9792. 
  9793. Received: by charon.cwi.nl with SMTP; Fri, 7 Feb 1992 00:18:09 +0100
  9794. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa19071;
  9795.           6 Feb 92 18:17 EST
  9796. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  9797.     id AA09617; Thu, 6 Feb 92 18:08:49 EST
  9798. Date: Thu, 6 Feb 92 18:08:49 EST
  9799. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  9800. Message-Id: <9202062308.AA09617@aemsun.med.Virginia.EDU>
  9801. To: Guido.van.Rossum@cwi.nl
  9802. Subject: Re: Python portability planning ( and prototypes )
  9803.  
  9804. > >Where pathname need to be written into a program, then it is not
  9805. > >unreasonable to expect the programmer to explicitly use a 
  9806. > >"path.to_native" conversion function, to indicate that he doesn't
  9807. > >actually MIND if the pathname get's munged up ( truncated | character
  9808. > >translated | etc. ) as long as there is a determinate mapping. 
  9809. > >( for practical purposes one-to-one )
  9810. > I don't understand this.  Pathnames hardcoded into programs are almost
  9811. > always things like /etc/termcap, /usr/tmp or the equivalent of
  9812. > $HOME/.mh_profile.  How would you expect a path.to_native for Mac or
  9813. > MS-DOS to translate these?  You can *never* expect to be able to move
  9814. > a program containing hardcoded pathnames to such a system without
  9815. > having to edit them.
  9816.  
  9817. One case that is easy: Files that go into /tmp on unix should probably
  9818. go into \tmp on my dos machine. ( \tmp is not a necessary part of the
  9819. dos file tree, but quite a number of programs require it, or some logical
  9820. assignment of TEMP. I can think up other cases, but most of them are 
  9821. a little more strained in terms of what is "reasonable" translation. 
  9822. One other case that is not would be with relative pathnames in a package
  9823. that expected files to be in a definite "relative" path. ( Like for 
  9824. example Python's own import search path! ). [ Actually, if you stick 
  9825. to common denominator characters, you can already do this with the
  9826. path module. ]. The user may be expected to change the top level 
  9827. directory, but not the whole set of path names used. 
  9828.  
  9829. > Filenames (i.e. no slash on UNIX) are a different matter, but even
  9830. > there the choice of names usually has to be revised by a human being
  9831. > when moving to a different O.S., e.g. names begginning with a . are
  9832. > not usable on MS-DOS.
  9833.  
  9834. But, yes: *FILE* names, rather than *PATH* names are where MOST of the
  9835. translation is useful. Even useful interactively - some of us who jump
  9836. from system to system would RATHER be able to slip up and write a 
  9837. unix style pathname and have it translated instead of getting an error.
  9838. But, again: I'm not asking for that as an automatic feature. Someone
  9839. should have to explicitly invoke it or explicitly alias open() to a 
  9840. wrapper that does it. It should NOT be a default action to rename    
  9841. peoples files into something else without warning them!!! 
  9842.  
  9843.  
  9844. Bye - I've got to go celebrate Chinese New Year ( a few days late ) ! 
  9845. - Steve
  9846.  
  9847. 
  9848. 
  9849. Received: by charon.cwi.nl with SMTP; Fri, 7 Feb 1992 09:47:39 +0100
  9850. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  9851.     (5.61/UUNET-internet-primary) id AA01764; Fri, 7 Feb 92 03:46:53 -0500
  9852. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  9853.     (queueing-rmail) id 023139.5112; Fri, 7 Feb 1992 02:31:39 EST
  9854. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  9855.     id AA02685; Fri, 7 Feb 92 02:21:08 EST
  9856. Received: by kaos.ksr.com (4.0/SMI-3.2)
  9857.     id AA09248; Fri, 7 Feb 92 02:21:07 EST
  9858. Message-Id: <9202070721.AA09248@kaos.ksr.com>
  9859. To: python-list@cwi.nl
  9860. Subject: And more stuff added to Emacs Python mode ...
  9861. Date: Fri, 07 Feb 92 02:21:06 EST
  9862. From: Tim Peters <tim@ksr.com>
  9863.  
  9864. Rather than burden y'all with a gazillabyte of new&improved Python-mode
  9865. every day, I'll just send the latest change log, and not broadcast the
  9866. code again until it settles down for a week.  Happy to send intermediate
  9867. versions on request (& if "too many" people request it, I'll just
  9868. broadcast it to the list instead).
  9869.  
  9870. The focus of the latest version is to allow blank lines & comment lines
  9871. "in the middle" without losing track of the proper indentation.  E.g.,
  9872.  
  9873. if whatever:
  9874.     print 'indenting this right is no trick'
  9875.  
  9876.             # some blank lines & a strangely-placed comment line
  9877.  
  9878.     print 'but this is'
  9879.  
  9880. Python mode handles this now.
  9881.  
  9882. The idea is that if Python accepts it without griping, so should Python-
  9883. mode.  The bulk of the changes actually had to do with esoteric
  9884. consequences of that principle, involving the sometimes darned-hard-to-
  9885. parse-backwards <grin> interactions among indentation, comment symbols,
  9886. and backslashes.  A couple examples of pathologies are given in the
  9887. change log below.
  9888.  
  9889. Uncomfortable but unavoidable side effect:  Suppose you type:
  9890.  
  9891. def oneplus(n):
  9892.     return 1+n
  9893. <an empty line>
  9894.  
  9895. The new Python mode, unlike the previous ones (which looked only at the
  9896. immediately preceding line), thinks the next line you type should be
  9897. indented to match the "return" line.  There's no general way to stop
  9898. this and still allow blank/empty lines where you want them -- the
  9899. paradox of Python-mode is that it wants to help you indent, but in
  9900. effect it needs you to tell it the proper indentation first since
  9901. there's no other way to figure out the grouping you intend (most other
  9902. language modes can figure out the grouping by looking at keywords and
  9903. bracketing constructs (begin/end if/endif {/} (/) etc) ...).
  9904.  
  9905. Question:  in looking over the to-do's at the bottom of the code, I
  9906. don't think I want to do this one:
  9907.  
  9908. - switch back to previous buffer when starting shell
  9909.  
  9910. That is, after "C-c !", I *like* winding up in the Python buffer (the
  9911. current behavior).  How do other people feel about that?
  9912.  
  9913. and-about-the-portability-thread-all-i-can-say-is-that-if-god-wanted-
  9914.    us-to-move-programs-from-machine-to-machine-he-wouldn't-have-
  9915.    invented-heart-disease<snort>-ly y'rs  - tim
  9916.  
  9917. Tim Peters   Kendall Square Research Corp
  9918. tim@ksr.com,         ksr!tim@uunet.uu.net
  9919.  
  9920. Latest python-mode.el change-log entry:
  9921.  
  9922. taught py-compute-indentation about blank & comment lines:
  9923.       accept comment line indentation as-is
  9924.       skip blank & comment lines when looking for the indentation of
  9925.           the preceding statement
  9926.       change py-shift-region docs accordingly
  9927. reversed order of change log (now most-recent first)
  9928. changed "\C-c\C-i" to "\C-c\t" for clarity
  9929. rearranged mode-map & syntax-table code to match other language modes
  9930. taught py-continuation-line-p that a trailing backslash does not
  9931.       indicate continuation when it ends a comment line
  9932. ditto py-goto-initial-line
  9933. fixed some pathological cases; e.g.,
  9934.       if \
  9935.         1 \
  9936.         :\
  9937.         \
  9938.         \
  9939.         # continued comment
  9940.       print 'I should be indented'
  9941.       if a == b : # \
  9942.       print 'I should be indented, but not like a continuation'
  9943. added consts py-stringlit-re and py-continued-re to support the above;
  9944.       made py-colon-line-re even hairier
  9945.  
  9946. >>> END OF MSG
  9947. 
  9948. 
  9949. Received: by charon.cwi.nl with SMTP; Fri, 7 Feb 1992 16:33:09 +0100
  9950. Received: by voorn.cwi.nl with SMTP; Fri, 7 Feb 1992 15:33:08 GMT
  9951. Message-Id: <9202071533.AA23653@voorn.cwi.nl>
  9952. To: python-list@cwi.nl
  9953. Subject: writing a Python extension module in C++
  9954. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  9955. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  9956. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  9957. Date: Fri, 07 Feb 1992 16:33:07 +0100
  9958. Sender: Guido.van.Rossum@cwi.nl
  9959.  
  9960. I've forgotten who asked about this, but I've just verified that it is
  9961. indeed possible to implement a Python extension module in C++.
  9962.  
  9963. All you really need to do is enclose the #includes of Python header
  9964. files in extern "C" { ... } and declare the only public C function of
  9965. the module (its initialization function) as extern "C".  Here's an
  9966. example ("trialmodule.C"):
  9967.  
  9968. ------------------------------------------------------------------------
  9969. // An extension module for Python in C++
  9970.  
  9971. extern "C" {
  9972.     #include "allobjects.h"
  9973.     #include "modsupport.h"
  9974. }
  9975.  
  9976. static object* trial_test(object*, object* args) {
  9977.     if (!getnoarg(args)) NULL;
  9978.     printf("Hello, world!\n");
  9979.     INCREF(None);
  9980.     return None;
  9981. }
  9982.  
  9983. static struct methodlist trial_methods[] = {
  9984.     {"test",    (method)trial_test}, // *** cast! ***
  9985.     {NULL,        NULL}
  9986. };
  9987.  
  9988. extern "C" void inittrial() {
  9989.     initmodule("trial", trial_methods);
  9990. }
  9991. ------------------------------------------------------------------------
  9992.  
  9993. There's one bit of uneasiness: the function pointer (trial_test) in
  9994. the initializer for trial_methods must be cast explicitly to method.
  9995. This is because the typedef for method in "methodobject.h" uses the
  9996. FPROTO() macro rather than PROTO() (both defined in "PROTO.h") which
  9997. makes the function prototype vanish, and C++ then believes it has no
  9998. arguments.  I may fix this in the next release.
  9999.  
  10000. It even works with the new dynamic loading I've added to Python 0.9.6!
  10001.  
  10002. Disclaimer: this is on an SGI with a reasonably modern C++ translator,
  10003. based on AT&T cfront 2.0.  I suppose a restriction will be that you
  10004. can't declare global objects with constructors or destructors, since
  10005. the Python main program is compiled by C, not by C++.
  10006.  
  10007. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  10008. Founder of the Royal Society for Prevention of Cruelty to Amoebae
  10009. 
  10010. 
  10011. To: python-list@cwi.nl
  10012. Subject: writing a Python extension module in C++
  10013. From: Guido van Rossum <guido@cwi.nl>
  10014. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  10015. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  10016. Date: Fri, 07 Feb 1992 16:33:07 +0100
  10017. Sender: guido
  10018.  
  10019. I've forgotten who asked about this, but I've just verified that it is
  10020. indeed possible to implement a Python extension module in C++.
  10021.  
  10022. All you really need to do is enclose the #includes of Python header
  10023. files in extern "C" { ... } and declare the only public C function of
  10024. the module (its initialization function) as extern "C".  Here's an
  10025. example ("trialmodule.C"):
  10026.  
  10027. ------------------------------------------------------------------------
  10028. // An extension module for Python in C++
  10029.  
  10030. extern "C" {
  10031.     #include "allobjects.h"
  10032.     #include "modsupport.h"
  10033. }
  10034.  
  10035. static object* trial_test(object*, object* args) {
  10036.     if (!getnoarg(args)) NULL;
  10037.     printf("Hello, world!\n");
  10038.     INCREF(None);
  10039.     return None;
  10040. }
  10041.  
  10042. static struct methodlist trial_methods[] = {
  10043.     {"test",    (method)trial_test}, // *** cast! ***
  10044.     {NULL,        NULL}
  10045. };
  10046.  
  10047. extern "C" void inittrial() {
  10048.     initmodule("trial", trial_methods);
  10049. }
  10050. ------------------------------------------------------------------------
  10051.  
  10052. There's one bit of uneasiness: the function pointer (trial_test) in
  10053. the initializer for trial_methods must be cast explicitly to method.
  10054. This is because the typedef for method in "methodobject.h" uses the
  10055. FPROTO() macro rather than PROTO() (both defined in "PROTO.h") which
  10056. makes the function prototype vanish, and C++ then believes it has no
  10057. arguments.  I may fix this in the next release.
  10058.  
  10059. It even works with the new dynamic loading I've added to Python 0.9.6!
  10060.  
  10061. Disclaimer: this is on an SGI with a reasonably modern C++ translator,
  10062. based on AT&T cfront 2.0.  I suppose a restriction will be that you
  10063. can't declare global objects with constructors or destructors, since
  10064. the Python main program is compiled by C, not by C++.
  10065.  
  10066. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  10067. Founder of the Royal Society for Prevention of Cruelty to Amoebae
  10068. 
  10069. 
  10070. Received: by charon.cwi.nl with SMTP; Sat, 8 Feb 1992 01:03:03 +0100
  10071. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  10072.     (5.61/UUNET-internet-primary) id AA01477; Fri, 7 Feb 92 19:02:12 -0500
  10073. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  10074.     (queueing-rmail) id 180141.17588; Fri, 7 Feb 1992 18:01:41 EST
  10075. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  10076.     id AA09230; Fri, 7 Feb 92 17:48:55 EST
  10077. Received: by kaos.ksr.com (4.0/SMI-3.2)
  10078.     id AA14716; Fri, 7 Feb 92 17:48:54 EST
  10079. Message-Id: <9202072248.AA14716@kaos.ksr.com>
  10080. To: python-list@cwi.nl
  10081. Subject: Emacs Python mode again
  10082. Date: Fri, 07 Feb 92 17:48:51 EST
  10083. From: Tim Peters <tim@ksr.com>
  10084.  
  10085. Added another function, to reformat existing code (to use the value of
  10086. py-indent-offset you like).  Easiest thing is to visit a file of old
  10087. code, then do
  10088.  
  10089.     C-x h C-c r
  10090.  
  10091. "C-x h" is ordinary Emacs that sets the region to the whole buffer, then
  10092. "C-c r" runs the new function py-reindent-top-level-region.  Here's the
  10093. doc string:
  10094.  
  10095.   "Reindent region with current value of py-indent-offset.
  10096. This is useful when you inherit Python code from someone who used
  10097. a silly (i.e., not yours) value for py-indent-offset, or just to see
  10098. what your code would look like if you changed your setting of py-
  10099. indent-offset.
  10100.  
  10101. The region is assumed to be correctly indented and to start with top-
  10102. level Python code (i.e., code that starts in the first column).  The
  10103. region is then reindented with the current value of py-indent-offset.
  10104.  
  10105. If you specify a numeric prefix argument, that will be used for the
  10106. indentation offset instead.
  10107.  
  10108. Special cases:  whitespace is deleted from entirely blank lines; and
  10109. continuation lines are shifted by the same amount their base line was
  10110. shifted, in order to preserve their relative indentation with respect to
  10111. their base line."
  10112.  
  10113. shiftily y'rs  - tim
  10114.  
  10115. Tim Peters   Kendall Square Research Corp
  10116. tim@ksr.com,         ksr!tim@uunet.uu.net
  10117. 
  10118. 
  10119. Replied: Mon, 10 Feb 1992 10:49:24 +0100
  10120. Replied: "Tim Peters <tim@ksr.com> +inbox"
  10121. Received: by charon.cwi.nl with SMTP; Mon, 10 Feb 1992 00:02:19 +0100
  10122. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  10123.     (5.61/UUNET-internet-primary) id AA01492; Sun, 9 Feb 92 18:02:17 -0500
  10124. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  10125.     (queueing-rmail) id 180123.29583; Sun, 9 Feb 1992 18:01:23 EST
  10126. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  10127.     id AA26267; Sun, 9 Feb 92 16:23:47 EST
  10128. Received: by kaos.ksr.com (4.0/SMI-3.2)
  10129.     id AA01145; Sun, 9 Feb 92 16:23:46 EST
  10130. Message-Id: <9202092123.AA01145@kaos.ksr.com>
  10131. To: python-list@cwi.nl
  10132. Subject: Finally happy w/ indentation in Emacs Python mode
  10133. Date: Sun, 09 Feb 92 16:23:45 EST
  10134. From: Tim Peters <tim@ksr.com>
  10135.  
  10136. New version attached.  Give it a try, gripe at will.
  10137.  
  10138. C-c r has gone away, and an extended version of its functionality merged
  10139. into C-c TAB.  The mode blurb (C-h m) explanation of indentation has
  10140. been completely rewritten; read it, and also do `C-h k C-c TAB' to learn
  10141. about C-c TAB's fine points.
  10142.  
  10143. > [a funny guido .sig]
  10144. > "A quick survey of the literature will show that there are hundreds of
  10145. > language features to support abstract data types, but only one
  10146. > example--the stack" --Mark R. Brown and Greg Nelson
  10147.  
  10148. C-c TAB is now smart enough to bail you out after pasting together
  10149. Python code from a variety of sources using a variety of indentation
  10150. conventions.  E.g. `C-x h C-c TAB' will change this:
  10151.  
  10152. >>> BEGIN CONTRIVED EXAMPLE
  10153.  
  10154. EmptyStack = 'attempt to access empty stack'
  10155.  
  10156. class Stack:
  10157.  
  10158.   def init(s):
  10159.     s.data = []
  10160.     return s
  10161.  
  10162.   def push(s,v): s.data.append(v)
  10163.  
  10164.   def top(s):
  10165.       if len(s.data) = 0:
  10166.                   raise \
  10167.                  EmptyStack
  10168.       else:
  10169.       return s.data[-1]
  10170.  
  10171.   def pop(s):
  10172.    top = s.top()
  10173.    del s.data[-1:]
  10174.    return top
  10175.  
  10176. >>> END CONTRIVED EXAMPLE
  10177.  
  10178. into this (when py-indent-offset is 4, or 4 is given as a prefix arg to
  10179. C-c TAB):
  10180.  
  10181. >>> BEGIN TRANSFORMED CONTRIVED EXAMPLE
  10182.  
  10183. EmptyStack = 'attempt to access empty stack'
  10184.  
  10185. class Stack:
  10186.  
  10187.     def init(s):
  10188.     s.data = []
  10189.     return s
  10190.  
  10191.     def push(s,v): s.data.append(v)
  10192.  
  10193.     def top(s):
  10194.     if len(s.data) = 0:
  10195.         raise \
  10196.          EmptyStack
  10197.     else:
  10198.         return s.data[-1]
  10199.  
  10200.     def pop(s):
  10201.     top = s.top()
  10202.     del s.data[-1:]
  10203.     return top
  10204.  
  10205. >>> END TRANSFORMED CONTRIVED EXAMPLE
  10206.  
  10207. The meaning of a prefix argument to C-C TAB has also changed:  it used
  10208. to mean "shift everything in the region by that amount"; now it's used
  10209. as a temporary value for py-indent-offset.  C-c < and C-c > are the only
  10210. remaining ways to shift a region rigidly; C-c TAB now always
  10211. "normalizes" region indentation.
  10212.  
  10213. it's-easier-to-use-than-to-explain-ly y'rs  - tim
  10214.  
  10215. Tim Peters   Kendall Square Research Corp
  10216. tim@ksr.com,         ksr!tim@uunet.uu.net
  10217.  
  10218. ;;; Major mode for editing Python programs.
  10219. ;; by: Michael A. Guravage and Guido van Rossum <guido@cwi.nl>
  10220. ;;
  10221. ;; The following statements, placed in your .emacs file or site-init.el,
  10222. ;; will cause this file to be autoloaded, and python-mode invoked, when
  10223. ;; visiting .py files (assuming the file is in your load-path):
  10224. ;;
  10225. ;;    (autoload 'python-mode "python-mode" "" t)
  10226. ;;    (setq auto-mode-alist
  10227. ;;          (cons '("\\.py$" . python-mode) auto-mode-alist))
  10228.  
  10229. ;;; Change log:
  10230. ;
  10231. ; Sun Feb  9 02:35:11 1992  tim
  10232. ; changed py-reindent-top-level-region so that it isn't confused by
  10233. ;    seeing a mixture of different indentation styles in the region
  10234. ; fixed potential infinite loop in py-reindent-top-level-region (if
  10235. ;    the region doesn't end with a newline, (forward-line 1) always
  10236. ;    returns a success value)
  10237. ; merged py-shift-region and py-reindent-top-level-region and renamed
  10238. ;    the combo py-indent-region, bound to C-c TAB
  10239. ; completely rewrote the mode blurb's explanation of indentation
  10240. ;
  10241. ; Fri Feb  7 17:18:11 1992  tim
  10242. ; added support for reindenting a region with a different value for
  10243. ;    the indentation offset; new function py-reindent-top-level-region;
  10244. ;    new docs; bound to C-c r.
  10245. ; replaced the hard-coded tabs in the mode blurb with "\t"
  10246. ;
  10247. ; Fri Feb  7 01:13:08 1992  tim
  10248. ; taught py-compute-indentation about blank & comment lines:
  10249. ;    accept comment line indentation as-is
  10250. ;    skip blank & comment lines when looking for the indentation of
  10251. ;        the preceding statement
  10252. ;    change py-shift-region docs accordingly
  10253. ; reversed order of change log (now most-recent first)
  10254. ; changed "\C-c\C-i" to "\C-c\t" for clarity
  10255. ; rearranged mode-map & syntax-table code to match other language modes
  10256. ; taught py-continuation-line-p that a trailing backslash does not
  10257. ;    indicate continuation when it ends a comment line
  10258. ; ditto py-goto-initial-line
  10259. ; fixed some pathological cases; e.g.,
  10260. ;    if \
  10261. ;          1 \
  10262. ;          :\
  10263. ;          \
  10264. ;          \
  10265. ;          # continued comment
  10266. ;        print 'I should be indented'
  10267. ;    if a == b : # \
  10268. ;        print 'I should be indented, but not like a continuation'
  10269. ; added consts py-stringlit-re and py-continued-re to support the above;
  10270. ;    made py-colon-line-re even hairier
  10271. ;
  10272. ; Wed Feb  5 21:52:15 1992  tim
  10273. ; changed new doc strings so first line makes sense on its own
  10274. ; new function py-shift-region; bound to C-c TAB
  10275. ; new function py-shift-region-left; bound to C-c <
  10276. ; new function py-shift-region-right; bound to C-c >
  10277. ; reorganized mode blurb
  10278. ; sped up py-continuation-line-p
  10279. ;
  10280. ; Wed Feb  5 03:23:31 1992  tim
  10281. ; added support for auto-indenting of continuation lines:
  10282. ;    new vrbl py-continuation-offset
  10283. ;    new function py-continuation-line-p
  10284. ;    new function py-goto-initial-line
  10285. ;    new function py-compute-indentation
  10286. ;    rewrote py-indent-line
  10287. ; changed py-indent-line to refrain from modifying the buffer if the
  10288. ;    indentation is already correct
  10289. ; hid the hairy colon-line regexp in a const
  10290. ; changed indent-region example to use legal Python
  10291. ; documented all that
  10292. ;
  10293. ; Mon Feb  3 20:37:27 1992  tim
  10294. ; renamed file to 'python-mode.el' for consistency with other Emacs
  10295. ;    language modes; changed autoload instructions accordingly
  10296. ; improved accuracy of new/changed docs
  10297. ; changed py-python-command from defconst to defvar so .emacs can
  10298. ;    override it if desired
  10299. ; add warning about indent-region; suggest indent-rigidly
  10300. ;
  10301. ; Sun Feb  2 21:48:59 1992  tim
  10302. ; renamed 'python-indent' to 'py-indent-offset' for internal consistency
  10303. ; replaced regexp in py-indent-line so it no longer indents after
  10304. ;    lines like:
  10305. ;    a = b   # ok: # found
  10306. ;    a = ': #'
  10307. ;
  10308. ; Sun Feb  2 02:08:47 1992  tim
  10309. ; added support for user-defined indentation increment:
  10310. ;     added python-indent variable
  10311. ;    changed py-indent-line to indent python-indent columns
  10312. ;    fixed small bug in py-indent-line (changed start of r.e.
  10313. ;        from [^#] to [^#\n])
  10314. ;    added py-delete-char function; bound to \177
  10315. ;    changed mode blurb accordingly
  10316.  
  10317.  
  10318. (provide 'python)
  10319.  
  10320. ;;; Constants and variables
  10321.  
  10322. (defvar py-python-command "python"
  10323.   "*UNIX shell command used to start Python interpreter")
  10324.  
  10325. (defvar py-indent-offset 4
  10326.   "*Indentation increment in Python mode")
  10327.  
  10328. (defvar py-continuation-offset 2
  10329.   "*Indentation (in addition to py-indent-offset) for continued lines")
  10330.  
  10331. (defvar py-mode-map nil "Keymap used in Python mode buffers")
  10332. (if py-mode-map
  10333.     ()
  10334.   (setq py-mode-map (make-sparse-keymap))
  10335.   (define-key py-mode-map "\C-c\C-c"    'py-execute-buffer)
  10336.   (define-key py-mode-map "\C-c|"    'py-execute-region)
  10337.   (define-key py-mode-map "\C-c!"    'py-shell)
  10338.   (define-key py-mode-map "\177"    'py-delete-char)
  10339.   (define-key py-mode-map "\C-c\t"    'py-indent-region)
  10340.   (define-key py-mode-map "\C-c<"    'py-shift-region-left)
  10341.   (define-key py-mode-map "\C-c>"    'py-shift-region-right))
  10342.  
  10343. (defvar py-mode-syntax-table nil "Python mode syntax table")
  10344. (if py-mode-syntax-table
  10345.     ()
  10346.   (setq py-mode-syntax-table (make-syntax-table))
  10347.   (set-syntax-table py-mode-syntax-table)
  10348.   (modify-syntax-entry ?\( "()")
  10349.   (modify-syntax-entry ?\) ")(")
  10350.   (modify-syntax-entry ?\[ "(]")
  10351.   (modify-syntax-entry ?\] ")[")
  10352.   (modify-syntax-entry ?\{ "(}")
  10353.   (modify-syntax-entry ?\} "){")
  10354.   (modify-syntax-entry ?\_ "w")
  10355.   (modify-syntax-entry ?\' "\"") ; single quote is string quote
  10356.   (modify-syntax-entry ?\` "$")     ; backquote is open and close paren
  10357.   (modify-syntax-entry ?\# "<")     ; hash starts comment
  10358.   (modify-syntax-entry ?\n ">")) ; newline ends comment
  10359.  
  10360. ;; a statement in Python opens a new block iff it ends with a colon;
  10361. ;; while conceptually trivial, quoted strings, continuation lines, and
  10362. ;; comments make this hard.  E.g., consider the statement
  10363. ;; if \
  10364. ;;      1 \
  10365. ;;      :\
  10366. ;;      \
  10367. ;;      \
  10368. ;;      # comment
  10369. ;; here we define some regexps to help
  10370.  
  10371. (defconst py-stringlit-re "'\\([^'\n\\]\\|\\\\.\\)*'"
  10372.   "regexp matching a Python string literal")
  10373.  
  10374. ;; warning!:  when [^#'\n\\] was written as [^#'\n\\]+ (i.e., with a
  10375. ;; '+' suffix), this appeared to run 100x slower in some bad cases.
  10376. (defconst py-colon-line-re
  10377.   (concat
  10378.     "\\(" "[^#'\n\\]" "\\|" py-stringlit-re "\\|" "\\\\\n" "\\)*"
  10379.     ":"
  10380.     "\\(" "[ \t]\\|\\\\\n" "\\)*"
  10381.     "\\(#.*\\)?" "$")
  10382.   "regexp matching Python statements opening a new block")
  10383.  
  10384. ;; this is tricky because a trailing backslash does not mean
  10385. ;; continuation if it's in a comment
  10386. (defconst py-continued-re
  10387.   (concat
  10388.    "\\(" "[^#'\n\\]" "\\|" py-stringlit-re "\\)*"
  10389.    "\\\\$")
  10390.   "regexp matching Python lines that are continued")
  10391.  
  10392. ;;; General Functions
  10393.  
  10394. (defun python-mode nil
  10395.   "Major mode for editing Python files.
  10396.  
  10397. Paragraphs are separated by blank lines only.
  10398.  
  10399. \\[python-mode] calls the value of the variable py-mode-hook with no args,
  10400. if that value is non-nil.
  10401.  
  10402. INTERFACE TO PYTHON INTERPRETER
  10403.  
  10404. \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
  10405. \\[py-execute-region]\tsends the current region.
  10406. \\[py-shell]\tstarts a Python interpreter window; this will be used by
  10407. \tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
  10408.  
  10409. VARIABLES
  10410.  
  10411. py-indent-offset\tindentation increment
  10412. py-continuation-offset\textra indentation given to continuation lines
  10413.  
  10414. py-continuation-offset is the additional indentation given to the first
  10415. continuation line in a multi-line statement.  Each subsequent
  10416. continuation line in the statement inherits its indentation from the
  10417. line that precedes it, so if you don't like the default indentation
  10418. given to the first continuation line, change it to something you do like
  10419. and Python-mode will automatically use that for the remaining
  10420. continuation lines (or, until you change the indentation again).
  10421.  
  10422. INDENTATION
  10423.  
  10424. Primarily for entering new code:
  10425. \t\\[indent-for-tab-command]\tindent line appropriately
  10426. \t\\[newline-and-indent]\tinsert newline, then indent
  10427. \t\\[py-delete-char]\treduce indentation, or delete single character
  10428.  
  10429. Primarily for reindenting existing code:
  10430. \t\\[py-indent-region]\treindent region to match its context
  10431. \t\\[py-shift-region-left]\tshift region left by py-indent-offset
  10432. \t\\[py-shift-region-right]\tshift region right by py-indent-offset
  10433.  
  10434. Unlike most programming languages, Python uses indentation, and only
  10435. indentation, to specify block structure.  Hence the indentation supplied
  10436. automatically by Python-mode is just an educated guess:  only you know
  10437. the block structure you intend, so only you can supply correct
  10438. indentation.
  10439.  
  10440. The \\[indent-for-tab-command] and \\[newline-and-indent] keys try to suggest plausible indentation, based on
  10441. the indentation of preceding statements.  E.g., assuming
  10442. py-indent-offset is 4, after you enter
  10443. \tif a > 0: \\[newline-and-indent]
  10444. the cursor will be moved to the position of the `x':
  10445. \tif a > 0:
  10446. \t    x
  10447. If you then enter `c = d' \\[newline-and-indent], the cursor will move
  10448. to
  10449. \tif a > 0:
  10450. \t    c = d
  10451. \t    x
  10452. Python-mode cannot know whether that's what you intended, or whether
  10453. \tif a > 0:
  10454. \t    c = d
  10455. \tx
  10456. was your intent.  In general, Python-mode either reproduces the
  10457. indentation of the preceding (non-blank and non-comment) statement, or
  10458. adds an extra py-indent-offset blanks if the preceding statement has
  10459. `:' as its last significant (non-whitespace and non-comment) character.
  10460.  
  10461. \\[py-delete-char] is handy after \\[newline-and-indent] to reduce excess indentation.  It reduces the
  10462. indentation of a line by py-indent-offset columns if point is at the
  10463. first non-blank character (if any) of a line, or at the end of an
  10464. entirely blank line; else it deletes the preceding character, converting
  10465. tabs to spaces as needed so that only one character position is deleted.
  10466.  
  10467. The remaining `indent' functions apply to a region of Python code.  They
  10468. assume the block structure (equals indentation, in Python) of the region
  10469. is correct, and alter the indentation in various ways while preserving
  10470. the block structure:
  10471.  
  10472. \\[py-indent-region] reindents a region to match its context and/or with a
  10473. different value for the indentation offset.  This is useful when code
  10474. blocks are moved or yanked, when enclosing control structures are
  10475. introduced or removed, or to reformat code using a new value for the
  10476. indentation offset.  See the function documentation for details.
  10477.  
  10478. Warning:  indent-region should not normally be used!  It calls \\[indent-for-tab-command]
  10479. repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
  10480. structure you intend.
  10481.  
  10482. \\[py-shift-region-left] shifts the region left by py-indent-offset columns.
  10483.  
  10484. \\[py-shift-region-right] shifts the region right by py-indent-offset columns.
  10485.  
  10486. The two `shift' functions above also honor numeric prefix arguments; see
  10487. the individual function documentation for details.
  10488.  
  10489. MODE MAP
  10490. \\{py-mode-map}"
  10491.  
  10492.   (interactive)
  10493.   (kill-all-local-variables)
  10494.   (use-local-map py-mode-map)
  10495.   (set-syntax-table py-mode-syntax-table)
  10496.   (setq  major-mode 'python-mode  mode-name "Python")
  10497.  
  10498.   (mapcar (function (lambda (x)
  10499.               (make-local-variable (car x))
  10500.               (set (car x) (cdr x))))
  10501.       '( (paragraph-separate . "^[ \t\f]*$")
  10502.          (paragraph-start     . "^[ \t\f]*$")
  10503.          (require-final-newline . t)
  10504.          (comment-start .        "# ")
  10505.          (comment-start-skip .    "# *")
  10506.          (comment-column . 40)
  10507.          (indent-line-function . py-indent-line)))
  10508.  
  10509.   (run-hooks 'py-mode-hook))
  10510.  
  10511.  
  10512. ;;; Functions that execute Python commands in a subprocess
  10513.  
  10514. (defun py-shell ()
  10515.   "Start an interactive Python interpreter in another window.
  10516. The variable py-python-command names the interpreter."
  10517.   (interactive)
  10518.   (require 'shell)
  10519.   (switch-to-buffer-other-window
  10520.    (make-shell "Python" py-python-command))
  10521.   (make-local-variable 'shell-prompt-pattern)
  10522.   (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. "))
  10523.  
  10524. (defun py-execute-region (start end)
  10525.   "Send the region between START and END to a Python interpreter.
  10526. If there is a *Python* process it is used."
  10527.   (interactive "r")
  10528.   (condition-case nil
  10529.       (process-send-string "Python" (buffer-substring start end))
  10530.     (error (shell-command-on-region start end py-python-command nil))))
  10531.  
  10532. (defun py-execute-buffer nil
  10533.   "Send the contents of the buffer to a Python interpreter.
  10534. If there is a *Python* process buffer it is used."
  10535.   (interactive)
  10536.   (py-execute-region (point-min) (point-max)))
  10537.  
  10538.  
  10539. ;;; Functions for Python style indentation
  10540.  
  10541. (defun py-delete-char ()
  10542.   "Reduce indentation or delete character.
  10543. If at first non-blank character, or at end of blank line, reduce
  10544. indentation by py-indent-offset columns.  Else delete preceding
  10545. character, converting tabs to spaces."
  10546.   (interactive)
  10547.   (backward-delete-char-untabify
  10548.    (if (and
  10549.     (= (current-indentation) (current-column))
  10550.     (>= (current-indentation) py-indent-offset))
  10551.        py-indent-offset
  10552.      1)))
  10553.  
  10554. (defun py-indent-line ()
  10555.   "Fix the indentation of the current line according to Python rules."
  10556.   (interactive)
  10557.   (let ( (need (py-compute-indentation)) )
  10558.     (if (= (current-indentation) need)
  10559.     nil
  10560.       (beginning-of-line)
  10561.       (delete-horizontal-space)
  10562.       (indent-to need))))
  10563.  
  10564. ;; go to first line of current statement; usually this is the line we're
  10565. ;; on, but if we're on the 2nd or following lines of a continuation
  10566. ;; block, we need to go up to the first line of the block
  10567. (defun py-goto-initial-line ()
  10568.   (while (py-continuation-line-p) (forward-line -1)))
  10569.  
  10570. ;; t iff on continuation line == preceding line ends with backslash
  10571. ;; that's not in a comment
  10572. (defun py-continuation-line-p ()
  10573.   (save-excursion
  10574.     (beginning-of-line)
  10575.     (and
  10576.      ;; use a cheap test first to avoid the regexp if possible
  10577.      ;; use 'eq' because char-after may return nil
  10578.      (eq (char-after (- (point) 2)) ?\\ )
  10579.      (progn
  10580.        (forward-line -1) ; since eq test passed, there is a line above
  10581.        (looking-at py-continued-re)))))
  10582.  
  10583. (defun py-compute-indentation ()
  10584.   (save-excursion
  10585.     (beginning-of-line)
  10586.     (cond
  10587.      ;; are we on a continuation line?
  10588.      ( (py-continuation-line-p)
  10589.        (forward-line -1)
  10590.        (if (py-continuation-line-p) ; on at least 3rd line in block
  10591.        (current-indentation)    ; so just continue the pattern
  10592.      ;; else on 2nd line in block, so indent more
  10593.      (+ (current-indentation) py-indent-offset
  10594.         py-continuation-offset)))
  10595.      ;; not on a continuation line
  10596.  
  10597.      ;; if at start of restriction, or on a comment line, assume they
  10598.      ;; intended whatever's there
  10599.      ( (or (bobp) (looking-at "[ \t]*#"))
  10600.        (current-indentation) )
  10601.  
  10602.      ;; else indentation based on that of the statement that precedes
  10603.      ;; us; use the first line of that statement to establish the base,
  10604.      ;; in case the user forced a non-std indentation for the
  10605.      ;; continuation lines (if any)
  10606.      ( t
  10607.        ;; skip back over blank & comment lines
  10608.        ;; note:  will skip a blank or comment line that happens to be
  10609.        ;; a continuation line too
  10610.        (re-search-backward "^[ \t]*[^ \t#\n]" (point-min) 1)
  10611.        (py-goto-initial-line)
  10612.        (if (looking-at py-colon-line-re)
  10613.        (+ (current-indentation) py-indent-offset)
  10614.      (current-indentation))))))
  10615.  
  10616. (defun py-shift-region (start end count)
  10617.   (save-excursion
  10618.     (goto-char end)   (beginning-of-line) (setq end (point))
  10619.     (goto-char start) (beginning-of-line) (setq start (point))
  10620.     (indent-rigidly start end count)))
  10621.  
  10622. (defun py-shift-region-left (start end &optional count)
  10623.   "Shift region of Python code to the left.
  10624. The lines from the line containing the start of the current region up
  10625. to (but not including) the line containing the end of the region are
  10626. shifted to the left, by py-indent-offset columns.
  10627.  
  10628. If a prefix argument is given, the region is instead shifted by that
  10629. many columns."
  10630.   (interactive "*r\nP")   ; region; raw prefix arg
  10631.   (py-shift-region start end
  10632.            (- (prefix-numeric-value
  10633.                (or count py-indent-offset)))))
  10634.  
  10635. (defun py-shift-region-right (start end &optional count)
  10636.   "Shift region of Python code to the right.
  10637. The lines from the line containing the start of the current region up
  10638. to (but not including) the line containing the end of the region are
  10639. shifted to the right, by py-indent-offset columns.
  10640.  
  10641. If a prefix argument is given, the region is instead shifted by that
  10642. many columns."
  10643.   (interactive "*r\nP")   ; region; raw prefix arg
  10644.   (py-shift-region start end (prefix-numeric-value
  10645.                   (or count py-indent-offset))))
  10646.  
  10647. (defun py-indent-region (start end &optional indent-offset)
  10648.   "Reindent a region of Python code.
  10649. The lines from the line containing the start of the current region up
  10650. to (but not including) the line containing the end of the region are
  10651. reindented.  If the first line of the region has a non-whitespace
  10652. character in the first column, the first line is left alone and the rest
  10653. of the region is reindented with respect to it.  Else the entire region
  10654. is reindented with respect to the (closest non-blank & non-comment)
  10655. statement immediately preceding the region.
  10656.  
  10657. This is useful when code blocks are moved or yanked, when enclosing
  10658. control structures are introduced or removed, or to reformat code using
  10659. a new value for the indentation offset.
  10660.  
  10661. If a numeric prefix argument is given, it will be used as the value of
  10662. the indentation offset.  Else the value of py-indent-offset will be
  10663. used.
  10664.  
  10665. Warning:  The region must be consistently indented before this function
  10666. is called!  This function does not compute proper indentation from
  10667. scratch (that's impossible in Python), it merely adjusts the existing
  10668. indentation to be correct in context.
  10669.  
  10670. Special cases:  whitespace is deleted from entirely blank lines;
  10671. continuation lines are shifted by the same amount their base line was
  10672. shifted, in order to preserve their relative indentation with respect to
  10673. their base line; and comment lines beginning in column 1 are ignored."
  10674.  
  10675.   (interactive "*r\nP") ; region; raw prefix arg
  10676.   (save-excursion
  10677.     (goto-char end)   (beginning-of-line) (setq end (point-marker))
  10678.     (goto-char start) (beginning-of-line)
  10679.     (let ( (py-indent-offset (prefix-numeric-value
  10680.                   (or indent-offset py-indent-offset)))
  10681.        (indents '(-1))    ; stack of active indent levels
  10682.        (target-column 0)    ; column to which to indent
  10683.        (base-shifted-by 0)    ; amount last base line was shifted
  10684.        (indent-base (if (looking-at "[ \t\n]")
  10685.                 (py-compute-indentation)
  10686.               0))
  10687.        ci)
  10688.       (while (< (point) end)
  10689.     (setq ci (current-indentation))
  10690.     ;; figure out appropriate target column
  10691.     (cond
  10692.      ( (or (eq (following-char) ?#)    ; comment in column 1
  10693.            (looking-at "[ \t]*$"))    ; entirely blank
  10694.        (setq target-column 0))
  10695.      ( (py-continuation-line-p)    ; shift relative to base line
  10696.        (setq target-column (+ ci base-shifted-by)))
  10697.      (t                ; new base line
  10698.       (if (> ci (car indents))    ; going deeper; push it
  10699.           (setq indents (cons ci indents))
  10700.         ;; else we should have seen this indent before
  10701.         (setq indents (memq ci indents)) ; pop deeper indents
  10702.         (if (null indents)
  10703.         (error "Bad indentation in region, at line %d"
  10704.                (save-restriction
  10705.              (widen)
  10706.              (1+ (count-lines 1 (point)))))))
  10707.       (setq target-column (+ indent-base
  10708.                  (* py-indent-offset
  10709.                     (- (length indents) 2))))
  10710.       (setq base-shifted-by (- target-column ci))))
  10711.     ;; shift as needed
  10712.     (if (/= ci target-column)
  10713.         (progn
  10714.           (delete-horizontal-space)
  10715.           (indent-to target-column)))
  10716.     (forward-line 1))))
  10717.   (set-marker end nil))
  10718.  
  10719. ;; To do:
  10720. ;; - add a newline when executing buffer ending in partial line
  10721. ;; - suppress prompts when executing regions
  10722. ;; - switch back to previous buffer when starting shell
  10723. ;; - support for ptags
  10724.  
  10725. >>> END OF MSG
  10726. 
  10727. 
  10728. Replied: Thu, 13 Feb 1992 11:02:36 +0100
  10729. Replied: "spm2d@jade.cs.Virginia.EDU +inbox"
  10730. Received: by charon.cwi.nl with SMTP; Thu, 13 Feb 1992 03:12:31 +0100
  10731. Received: from uvacs.cs.virginia.edu by uvaarpa.Virginia.EDU id aa28762;
  10732.           12 Feb 92 21:11 EST
  10733. Received: from jade.cs.Virginia.EDU by uvacs.cs.Virginia.EDU (4.1/5.1.UVA)
  10734.     id AA17939; Wed, 12 Feb 92 21:10:35 EST
  10735. Posted-Date: Wed, 12 Feb 92 21:10:22 EST
  10736. Return-Path: <spm2d@jade.cs.Virginia.EDU>
  10737. Received: by jade.cs.Virginia.EDU (4.1/SMI-2.0)
  10738.     id AA23722; Wed, 12 Feb 92 21:10:22 EST
  10739. Date: Wed, 12 Feb 92 21:10:22 EST
  10740. From: spm2d@jade.cs.Virginia.EDU
  10741. Message-Id: <9202130210.AA23722@jade.cs.Virginia.EDU>
  10742. To: python-list@cwi.nl
  10743. Subject: Tcl vs. Python
  10744.  
  10745. Has anyone run timing benchmarks? We are strongly considering a move
  10746. to Python, but wish to know what will change with respect to speed.
  10747.  
  10748. For your information, we plan to run multiple interpreters on a Sparc
  10749. which will be connected to 2 SGIs.
  10750.  
  10751. Thanks for your help,
  10752. Steve
  10753. 
  10754. 
  10755. To: spm2d@jade.cs.Virginia.EDU
  10756. Subject: Re: Tcl vs. Python 
  10757. In-reply-to: Your message of "Wed, 12 Feb 1992 21:10:22 MET."
  10758.              <9202130210.AA23722@jade.cs.Virginia.EDU> 
  10759. From: Guido van Rossum <guido@cwi.nl>
  10760. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  10761. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  10762. Date: Thu, 13 Feb 1992 11:02:37 +0100
  10763. Sender: guido
  10764.  
  10765. >Has anyone run timing benchmarks? We are strongly considering a move
  10766. >to Python, but wish to know what will change with respect to speed.
  10767.  
  10768. Long ago I compared the speed of a Python version of fac(n) with a
  10769. figures given by Ousterhout for an early version of Tcl.  On the only
  10770. machine to which both I and Ousterhout had access (a DECstation 3100)
  10771. execution of the Python version was about 3 times as fast.  The Python
  10772. function was this:
  10773.  
  10774.     def fac(n):
  10775.         if n == 1: return 1
  10776.         else: return n * fac(n-1)
  10777.  
  10778. and it took 1.3 msec/call; the equivalent Tcl (don't have the source
  10779. handy) took 3.6 msec/call.
  10780.  
  10781. I expect that a fair comparison of the speeds of Python and Tcl will
  10782. be hard: both have the property that complex operations (such as
  10783. regular expression matches) will run at the same speed as C (because
  10784. they are implemented in C), while simple things like "i = i+1" takes
  10785. forever compared to C.
  10786.  
  10787. Given what I know of the Tcl implementation, I expect that operations
  10788. using complex data will be slower in Tcl because they are represented
  10789. as strings that must be packed and unpacked (this is even true for
  10790. numbers).  Tcl also has to do more work decoding the commands because
  10791. they are parsed on the fly, during execution, while Python represents
  10792. them in binary.
  10793.  
  10794. However, Python programs take more time up front because the code is
  10795. parsed and `compiled' before it is executed.  Fortunately, the
  10796. compiled representation of a module is cached (for every module
  10797. "foo.py" there is a cache file "foo.pyc", saving a lot of parse time).
  10798. Also, Tcl uses tricks like allocating small buffers on the stack
  10799. instead of on the heap to reduce the number of malloc() and free()
  10800. calls, while Python allocates all objects except integers on the heap.
  10801. I can imagine that this might make a null routine call in Tcl faster
  10802. than the same in Python; however the figures of the 'fac' example
  10803. above (which is function call intensive) seem to imply that Python is
  10804. still faster.
  10805.  
  10806. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  10807. "You're so *digital*, girl!" -- Neneh Cherry
  10808. 
  10809. 
  10810. Replied: Thu, 13 Feb 1992 11:07:59 +0100
  10811. Replied: "Your message of "Thu, 13 Feb 1992 11:03:16 MET."
  10812. Replied: <9202131003.AA10240@voorn.cwi.nl>  python-list@cwi.nl"
  10813. Received: by charon.cwi.nl with SMTP; Thu, 13 Feb 1992 11:03:18 +0100
  10814. Received: by voorn.cwi.nl with SMTP; Thu, 13 Feb 1992 10:03:17 GMT
  10815. Message-Id: <9202131003.AA10240@voorn.cwi.nl>
  10816. To: python-list@cwi.nl
  10817. Subject: Re: Tcl vs. Python 
  10818. In-Reply-To: Your message of "Wed, 12 Feb 1992 21:10:22 MET."
  10819.              <9202130210.AA23722@jade.cs.Virginia.EDU> 
  10820. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  10821. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  10822. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  10823. Date: Thu, 13 Feb 1992 11:03:16 +0100
  10824. Sender: Guido.van.Rossum@cwi.nl
  10825.  
  10826. >Has anyone run timing benchmarks? We are strongly considering a move
  10827. >to Python, but wish to know what will change with respect to speed.
  10828.  
  10829. Long ago I compared the speed of a Python version of fac(n) with a
  10830. figures given by Ousterhout for an early version of Tcl.  On the only
  10831. machine to which both I and Ousterhout had access (a DECstation 3100)
  10832. execution of the Python version was about 3 times as fast.  The Python
  10833. function was this:
  10834.  
  10835.     def fac(n):
  10836.         if n == 1: return 1
  10837.         else: return n * fac(n-1)
  10838.  
  10839. and it took 1.3 msec/call; the equivalent Tcl (don't have the source
  10840. handy) took 3.6 msec/call.
  10841.  
  10842. I expect that a fair comparison of the speeds of Python and Tcl will
  10843. be hard: both have the property that complex operations (such as
  10844. regular expression matches) will run at the same speed as C (because
  10845. they are implemented in C), while simple things like "i = i+1" takes
  10846. forever compared to C.
  10847.  
  10848. Given what I know of the Tcl implementation, I expect that operations
  10849. using complex data will be slower in Tcl because they are represented
  10850. as strings that must be packed and unpacked (this is even true for
  10851. numbers).  Tcl also has to do more work decoding the commands because
  10852. they are parsed on the fly, during execution, while Python represents
  10853. them in binary.
  10854.  
  10855. However, Python programs take more time up front because the code is
  10856. parsed and `compiled' before it is executed.  Fortunately, the
  10857. compiled representation of a module is cached (for every module
  10858. "foo.py" there is a cache file "foo.pyc", saving a lot of parse time).
  10859. Also, Tcl uses tricks like allocating small buffers on the stack
  10860. instead of on the heap to reduce the number of malloc() and free()
  10861. calls, while Python allocates all objects except integers on the heap.
  10862. I can imagine that this might make a null routine call in Tcl faster
  10863. than the same in Python; however the figures of the 'fac' example
  10864. above (which is function call intensive) seem to imply that Python is
  10865. still faster.
  10866.  
  10867. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  10868. "You're so *digital*, girl!" -- Neneh Cherry
  10869. 
  10870. 
  10871. Received: by charon.cwi.nl with SMTP; Thu, 13 Feb 1992 11:08:03 +0100
  10872. Received: by voorn.cwi.nl with SMTP; Thu, 13 Feb 1992 10:08:01 GMT
  10873. Message-Id: <9202131008.AA10261@voorn.cwi.nl>
  10874. To: python-list@cwi.nl
  10875. Subject: Re: Tcl vs. Python 
  10876. In-Reply-To: Your message of "Thu, 13 Feb 1992 11:03:16 MET."
  10877.              <9202131003.AA10240@voorn.cwi.nl> 
  10878. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  10879. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  10880. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  10881. Date: Thu, 13 Feb 1992 11:08:00 +0100
  10882. Sender: Guido.van.Rossum@cwi.nl
  10883.  
  10884. Oops, I should have said that the benchmark called fac(5).
  10885. --Guido
  10886. 
  10887. 
  10888. Received: by charon.cwi.nl with SMTP; Mon, 17 Feb 1992 22:22:17 +0100
  10889. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  10890.     (5.61/UUNET-internet-primary) id AA27727; Mon, 17 Feb 92 16:22:14 -0500
  10891. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  10892.     (queueing-rmail) id 162136.2323; Mon, 17 Feb 1992 16:21:36 EST
  10893. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  10894.     id AA01015; Mon, 17 Feb 92 16:06:58 EST
  10895. Received: by kaos.ksr.com (4.0/SMI-3.2)
  10896.     id AA16586; Mon, 17 Feb 92 16:06:57 EST
  10897. Message-Id: <9202172106.AA16586@kaos.ksr.com>
  10898. To: python-list@cwi.nl
  10899. Subject: Version 1.0 of (Emacs) Python mode
  10900. Date: Mon, 17 Feb 92 16:06:56 EST
  10901. From: Tim Peters <tim@ksr.com>
  10902.  
  10903. Attached.  Many changes from the last version you saw, including an
  10904. obnoxious copyright notice that's as weak as possible without inviting
  10905. nuisance litigation.
  10906.  
  10907. The only major incompatibility with previous versions is that
  10908. py-indent-offset now defaults to 8.
  10909.  
  10910. New commands include:
  10911.     C-c C-n     go to next statement
  10912.     C-c C-p  go to previous statement
  10913.     LFD     in most ways unchanged
  10914.     C-c C-u     go to start of smallest enclosing code block
  10915.     C-c C-b  mark the next "interesting" block of lines
  10916.     C-c #     comment out a region of code
  10917.     C-u C-c #  uncomment a commented-out region of code
  10918.  
  10919. And almost everything else has changed in some small way or another in
  10920. response to various gripes.  Note especially that a convention has been
  10921. introduced for distinguishing between comments that should and shouldn't
  10922. affect the indentation of following lines.
  10923.  
  10924. The mode blurb (C-h m) has gotten huge; sorry about that.  An Emacs Info
  10925. node would be more appropriate, but I don't know how to write one &
  10926. don't have time to learn.
  10927.  
  10928. gripes-&-suggestions-welcome-ly y'rs  - tim
  10929.  
  10930. Tim Peters   Kendall Square Research Corp
  10931. tim@ksr.com,         ksr!tim@uunet.uu.net
  10932.  
  10933. The change log has been removed from the code.  Here are the entries
  10934. (most recent first) since the last version you saw:
  10935.  
  10936. Mon Feb 17 15:28:12 1992  tim
  10937. replaced '(point-min)' by 'nil' in a couple regexp search cmds (faster)
  10938. rewrote py-mark-block to be more useful
  10939. new helper py-goto-beyond-final-line
  10940. new const py-blank-or-comment-re
  10941. changed the docs accordingly
  10942. stuck in some `Emacs hints' since half the questions i get are
  10943.     really general Emacs questions
  10944. added copyright
  10945. added version number
  10946. removed change log
  10947.  
  10948. Sun Feb 16 01:03:32 1992  tim
  10949. changed 'provide' to provide 'python-mode, so that a plain
  10950.     (require 'python-mode) will work
  10951. changed binding of py-mark-block to C-c C-b (`C-c RET' was too confusing)
  10952. new function py-goto-block-up, bound to C-c C-u
  10953. changed py-newline-and-indent to try to act more like
  10954.     newline-and-indent usually acts in other language modes (obscure)
  10955.  
  10956. Sat Feb 15 00:05:33 1992  tim
  10957. minor code cleanups (no change in function)
  10958. removed old instances of \f, since Python doesn't accept formfeeds
  10959. changed py-compute-indentation to distinguish between two kinds
  10960.     of comment lines (indenting & non-indenting)
  10961. added to mode blurb, explaining the various terms used in the docs
  10962.     for talking about python lines & statements
  10963. changed all the docs to use those terms consistently
  10964. changed py-indent-offset default to 8, to humor Guido <grin>
  10965. added code to recognize embedded vi directive to change tabsize
  10966.     added new vrbl py-beep-if-tab-change
  10967. new function py-comment-region, bound to C-c #
  10968.     new supporting vrbl py-block-comment-prefix
  10969.  
  10970. Wed Feb 12 23:44:45 1992  tim
  10971. added function py-mark-block, bound to C-c C-m
  10972. added helper function py-suck-up-leading-text
  10973.  
  10974. Tue Feb 11 21:54:44 1992  tim
  10975. changed py-indent-line to leave point where the `Basic Indent' node
  10976.     of the Emacs docs sez TAB will leave it
  10977. added new function py-newline-and-indent, bound to LFD
  10978.          updated docs
  10979. added initialization code to search the global keymap for
  10980.     instances of newline-and-indent, and to shadow them
  10981.     by binding the same keys to py-newline-and-indent in
  10982.     the local py-mode-map (CAUTION:  not entirely sure I
  10983.     know all the implications)
  10984.  
  10985. Mon Feb 10 22:33:39 1992  tim
  10986. mentioned py-python-command in 'variable' section of mode blurb
  10987. fixed py-goto-initial-line so it always moves to start of line
  10988. rearranged some of the functions
  10989. added my name to the "who's to blame?" blurb
  10990. added new functions for moving point:
  10991.     py-goto-statement-at-or-above
  10992.     py-goto-statement-below
  10993.     py-next-statement; bound to C-c C-n
  10994.     py-previous-statement; bound to C-c C-p
  10995.  
  10996. And here's the code:
  10997.  
  10998. ;;; Major mode for editing Python programs, version 1.00
  10999. ;; by: Michael A. Guravage
  11000. ;;     Guido van Rossum <guido@cwi.nl>
  11001. ;;     Tim Peters <tim@ksr.com>
  11002. ;;
  11003. ;; Copyright (c) 1992  Tim Peters
  11004. ;;
  11005. ;; This software is provided as-is, without express or implied warranty.
  11006. ;; Permission to use, copy, modify, distribute or sell this software,
  11007. ;; without fee, for any purpose and by any individual or organization, is
  11008. ;; hereby granted, provided that the above copyright notice and this
  11009. ;; paragraph appear in all copies.
  11010. ;;
  11011. ;;
  11012. ;; The following statements, placed in your .emacs file or site-init.el,
  11013. ;; will cause this file to be autoloaded, and python-mode invoked, when
  11014. ;; visiting .py files (assuming the file is in your load-path):
  11015. ;;
  11016. ;;    (autoload 'python-mode "python-mode" "" t)
  11017. ;;    (setq auto-mode-alist
  11018. ;;          (cons '("\\.py$" . python-mode) auto-mode-alist))
  11019.  
  11020. (provide 'python-mode)
  11021.  
  11022. ;;; Constants and variables
  11023.  
  11024. (defvar py-python-command "python"
  11025.   "*UNIX shell command used to start Python interpreter")
  11026.  
  11027. (defvar py-indent-offset 8        ; argue with Guido <grin>
  11028.   "*Indentation increment in Python mode")
  11029.  
  11030. (defvar py-continuation-offset 2
  11031.   "*Indentation (in addition to py-indent-offset) for continued lines")
  11032.  
  11033. (defvar py-block-comment-prefix "##"
  11034.   "*String used by py-comment-region to comment out a block of code")
  11035.  
  11036. (defvar py-beep-if-tab-change t
  11037.   "*Ring the bell if tab-width is changed")
  11038.  
  11039. (defvar py-mode-map nil "Keymap used in Python mode buffers")
  11040. (if py-mode-map
  11041.     ()
  11042.   (setq py-mode-map (make-sparse-keymap))
  11043.  
  11044.   ;; shadow global bindings for newline-and-indent w/ the py- version
  11045.   (mapcar (function (lambda (key)
  11046.               (define-key
  11047.             py-mode-map key 'py-newline-and-indent)))
  11048.    (where-is-internal 'newline-and-indent))
  11049.  
  11050.   (define-key py-mode-map "\C-c\C-c"    'py-execute-buffer)
  11051.   (define-key py-mode-map "\C-c|"    'py-execute-region)
  11052.   (define-key py-mode-map "\C-c!"    'py-shell)
  11053.   (define-key py-mode-map "\177"    'py-delete-char)
  11054.   (define-key py-mode-map "\n"        'py-newline-and-indent)
  11055.   (define-key py-mode-map "\C-c\t"    'py-indent-region)
  11056.   (define-key py-mode-map "\C-c<"    'py-shift-region-left)
  11057.   (define-key py-mode-map "\C-c>"    'py-shift-region-right)
  11058.   (define-key py-mode-map "\C-c\C-n"    'py-next-statement)
  11059.   (define-key py-mode-map "\C-c\C-p"    'py-previous-statement)
  11060.   (define-key py-mode-map "\C-c\C-u"    'py-goto-block-up)
  11061.   (define-key py-mode-map "\C-c\C-b"    'py-mark-block)
  11062.   (define-key py-mode-map "\C-c#"    'py-comment-region))
  11063.  
  11064.  
  11065. (defvar py-mode-syntax-table nil "Python mode syntax table")
  11066. (if py-mode-syntax-table
  11067.     ()
  11068.   (setq py-mode-syntax-table (make-syntax-table))
  11069.   (set-syntax-table py-mode-syntax-table)
  11070.   (modify-syntax-entry ?\( "()")
  11071.   (modify-syntax-entry ?\) ")(")
  11072.   (modify-syntax-entry ?\[ "(]")
  11073.   (modify-syntax-entry ?\] ")[")
  11074.   (modify-syntax-entry ?\{ "(}")
  11075.   (modify-syntax-entry ?\} "){")
  11076.   (modify-syntax-entry ?\_ "w")
  11077.   (modify-syntax-entry ?\' "\"") ; single quote is string quote
  11078.   (modify-syntax-entry ?\` "$")     ; backquote is open and close paren
  11079.   (modify-syntax-entry ?\# "<")     ; hash starts comment
  11080.   (modify-syntax-entry ?\n ">")) ; newline ends comment
  11081.  
  11082. ;; a statement in Python opens a new block iff it ends with a colon;
  11083. ;; while conceptually trivial, quoted strings, continuation lines, and
  11084. ;; comments make this hard.  E.g., consider the statement
  11085. ;; if \
  11086. ;;      1 \
  11087. ;;      :\
  11088. ;;      \
  11089. ;;      \
  11090. ;;      # comment
  11091. ;; here we define some regexps to help
  11092.  
  11093. (defconst py-stringlit-re "'\\([^'\n\\]\\|\\\\.\\)*'"
  11094.   "regexp matching a Python string literal")
  11095.  
  11096. ;; warning!:  when [^#'\n\\] was written as [^#'\n\\]+ (i.e., with a
  11097. ;; '+' suffix), this appeared to run 100x slower in some bad cases.
  11098. (defconst py-colon-line-re
  11099.   (concat
  11100.     "\\(" "[^#'\n\\]" "\\|" py-stringlit-re "\\|" "\\\\\n" "\\)*"
  11101.     ":"
  11102.     "\\(" "[ \t]\\|\\\\\n" "\\)*"
  11103.     "\\(#.*\\)?" "$")
  11104.   "regexp matching Python statements opening a new block")
  11105.  
  11106. ;; this is tricky because a trailing backslash does not mean
  11107. ;; continuation if it's in a comment
  11108. (defconst py-continued-re
  11109.   (concat
  11110.    "\\(" "[^#'\n\\]" "\\|" py-stringlit-re "\\)*"
  11111.    "\\\\$")
  11112.   "regexp matching Python lines that are continued")
  11113.  
  11114. (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
  11115.   "regexp matching blank or comment lines")
  11116.  
  11117. ;;; General Functions
  11118.  
  11119. (defun python-mode ()
  11120.   "Major mode for editing Python files.
  11121.  
  11122. Paragraphs are separated by blank lines only.
  11123.  
  11124. \\[python-mode] calls the value of the variable py-mode-hook with no
  11125. args, if that value is non-nil.
  11126.  
  11127. Obscure:  When python-mode is first loaded, it looks for all bindings to
  11128. newline-and-indent in the global keymap, and shadows them with local
  11129. bindings to py-newline-and-indent.
  11130.  
  11131. INTERFACE TO PYTHON INTERPRETER
  11132.  
  11133. \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
  11134. \\[py-execute-region]\tsends the current region.
  11135. \\[py-shell]\tstarts a Python interpreter window; this will be used by
  11136. \tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
  11137.  
  11138. VARIABLES
  11139.  
  11140. If you don't like the default value for one of these, change its
  11141. value to whatever you do like by putting a `setq' line in your .emacs
  11142. file.  E.g., to set the indentation increment to 7, put this line in
  11143. your .emacs:
  11144. \t(setq  py-indent-offset  7)
  11145. To see the value of a variable, do `\\[describe-variable]' and enter the variable
  11146. name at the prompt.
  11147.  
  11148. py-python-command\tshell command to invoke Python interpreter
  11149. py-indent-offset\tindentation increment
  11150. py-continuation-offset\textra indentation given to continuation lines
  11151. py-block-comment-prefix\tcomment string used by `\\[py-comment-region]'
  11152. py-beep-if-tab-change\tring the bell if tab-width is changed
  11153.  
  11154. py-continuation-offset is the additional indentation given to the first
  11155. continuation line in a multi-line statement.  Each subsequent
  11156. continuation line in the statement inherits its indentation from the
  11157. line that precedes it, so if you don't like the default indentation
  11158. given to the first continuation line, change it to something you do like
  11159. and Python-mode will automatically use that for the remaining
  11160. continuation lines (or, until you change the indentation again).
  11161.  
  11162. If a comment of the form
  11163. \t# vi:set tabsize=<number>:
  11164. is found before the first code line when the file is entered, and
  11165. the current value of (the general Emacs variable) tab-width does not
  11166. equal <number>, tab-width is set to <number>, a message saying so is
  11167. displayed in the echo area, and if py-beep-if-tab-change is non-nil the
  11168. Emacs bell is also rung as a warning.
  11169.  
  11170. KINDS OF LINES
  11171.  
  11172. Each physical line in the file is either a `continuation line' (the
  11173. preceding line ends with a backslash that's not part of a comment) or an
  11174. `initial line' (everything else).
  11175.  
  11176. An initial line is in turn a `blank line' (contains nothing except
  11177. possibly blanks or tabs), a `comment line' (leftmost non-blank character
  11178. is `#'), or a `code line' (everything else).
  11179.  
  11180. Comment Lines
  11181.  
  11182. Although all comment lines are treated alike by Python, Python mode
  11183. recognizes two kinds that act differently with respect to indentation.
  11184.  
  11185. An `indenting comment line' is a comment line with a blank, tab or
  11186. nothing after the initial `#'.  The indentation commands (see below)
  11187. treat these exactly as if they were code lines:  a line following an
  11188. indenting comment line will be indented like the comment line.  All
  11189. other comment lines (those with a non-whitespace character immediately
  11190. following the initial `#') are `non-indenting comment lines', and their
  11191. indentation is ignored by the indentation commands.
  11192.  
  11193. Indenting comment lines are by far the usual case, and should be used
  11194. whenever possible.  Non-indenting comment lines are useful in cases like
  11195. these:
  11196.  
  11197. \ta = b   # a very wordy single-line comment that ends up being
  11198. \t        #... continued onto another line
  11199.  
  11200. \tif a == b:
  11201. ##\t\tprint 'panic!' # old code we've `commented out'
  11202. \t\treturn a
  11203.  
  11204. Since the `#...' and `##' comment lines have a non-whitespace character
  11205. following the initial `#', Python mode ignores them when computing the
  11206. proper indentation for the next line.
  11207.  
  11208. Continuation Lines and Statements
  11209.  
  11210. The Python-mode commands generally work on statements instead of on
  11211. individual lines, where a `statement' is a comment or blank line, or a
  11212. code line and all of its following continuation lines (if any)
  11213. considered as a single logical unit.  The commands in this mode
  11214. generally (when it makes sense) automatically move to the start of the
  11215. statement containing point, even if point happens to be in the middle of
  11216. some continuation line.
  11217.  
  11218. A Bad Idea
  11219.  
  11220. Always put something on the initial line of a multi-line statement
  11221. besides the backslash!  E.g., don't do this:
  11222.  
  11223. \t\\
  11224. \ta = b # what's the indentation of this stmt?
  11225.  
  11226. While that's legal Python, it's silly & would be very expensive for
  11227. Python mode to handle correctly.
  11228.  
  11229.  
  11230. INDENTATION
  11231.  
  11232. Primarily for entering new code:
  11233. \t\\[indent-for-tab-command]\t indent line appropriately
  11234. \t\\[py-newline-and-indent]\t insert newline, then indent
  11235. \t\\[py-delete-char]\t reduce indentation, or delete single character
  11236.  
  11237. Primarily for reindenting existing code:
  11238. \t\\[py-indent-region]\t reindent region to match its context
  11239. \t\\[py-shift-region-left]\t shift region left by py-indent-offset
  11240. \t\\[py-shift-region-right]\t shift region right by py-indent-offset
  11241.  
  11242. Unlike most programming languages, Python uses indentation, and only
  11243. indentation, to specify block structure.  Hence the indentation supplied
  11244. automatically by Python-mode is just an educated guess:  only you know
  11245. the block structure you intend, so only you can supply correct
  11246. indentation.
  11247.  
  11248. The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
  11249. the indentation of preceding statements.  E.g., assuming
  11250. py-indent-offset is 4, after you enter
  11251. \tif a > 0: \\[py-newline-and-indent]
  11252. the cursor will be moved to the position of the `x':
  11253. \tif a > 0:
  11254. \t    x
  11255. If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
  11256. to
  11257. \tif a > 0:
  11258. \t    c = d
  11259. \t    x
  11260. Python-mode cannot know whether that's what you intended, or whether
  11261. \tif a > 0:
  11262. \t    c = d
  11263. \tx
  11264. was your intent.  In general, Python-mode either reproduces the
  11265. indentation of the (closest code or indenting-comment) preceding
  11266. statement, or adds an extra py-indent-offset blanks if the preceding
  11267. statement has `:' as its last significant (non-whitespace and non-
  11268. comment) character.
  11269.  
  11270. \\[py-newline-and-indent] strives to `act like' the Emacs newline-and-indent function; this
  11271. requires some obscure Python-specific trickery because, as noted above,
  11272. it's not always possible to compute your intended indentation.
  11273.  
  11274. \\[py-delete-char] is handy after \\[py-newline-and-indent] to reduce excess indentation.  It reduces the
  11275. indentation of a line by py-indent-offset columns if point is at the
  11276. first non-blank character (if any) of a line, or at the end of a blank
  11277. line; else it deletes the preceding character, converting tabs to spaces
  11278. as needed so that only one character position is deleted.
  11279.  
  11280. The remaining `indent' functions apply to a region of Python code.  They
  11281. assume the block structure (equals indentation, in Python) of the region
  11282. is correct, and alter the indentation in various ways while preserving
  11283. the block structure:
  11284.  
  11285. `\\[py-indent-region]' reindents a region to match its context and/or with a
  11286. different value for the indentation offset.  This is useful when code
  11287. blocks are moved or yanked, when enclosing control structures are
  11288. introduced or removed, or to reformat code using a new value for the
  11289. indentation offset.  See the function documentation for details.
  11290.  
  11291. Warning:  indent-region should not normally be used!  It calls \\[indent-for-tab-command]
  11292. repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
  11293. structure you intend.
  11294.  
  11295. `\\[py-shift-region-left]' shifts the region left by py-indent-offset columns.
  11296.  
  11297. `\\[py-shift-region-right]' shifts the region right by py-indent-offset columns.
  11298.  
  11299. The two `shift' functions above also honor numeric prefix arguments; see
  11300. the individual function documentation for details.
  11301.  
  11302. MARKING & MANIPULATING REGIONS OF CODE
  11303.  
  11304. \\[py-mark-block]\t mark block of lines
  11305. \\[py-comment-region]\t comment out region of code
  11306. \\[universal-argument] \\[py-comment-region]\t uncomment region of code
  11307.  
  11308. `\\[py-mark-block]' is easier to use than explain.  It sets the region to an
  11309. `interesting' block of succeeding lines.  If point is on a blank line,
  11310. it goes down to the next non-blank line.  That will be the start of the
  11311. region.  The end of the region depends on the kind of line at the start:
  11312.  
  11313.  - If a comment, the region will include all succeeding comment lines up
  11314.    to (but not including) the next non-comment line (if any).
  11315.  
  11316.  - If a code line that opens a new block, the region will include all
  11317.    succeeding lines up to (but not including) the next code statement
  11318.    (if any) that's indented no more than the starting line, except that
  11319.    trailing blank and comment lines are excluded.  E.g., if the starting
  11320.    line is a `def' statement, the region will be set to the full
  11321.    function definition, but without any trailing `noise' lines.
  11322.  
  11323.  - Else the region will include all succeeding lines up to (but not
  11324.    including) the next blank line, or code or indenting-comment line
  11325.    indented strictly less than the starting line.  Trailing indenting
  11326.    comment lines are included in this case, but not trailing blank
  11327.    lines.
  11328.  
  11329. `\\[py-comment-region]' comments out a region of code by inserting the string
  11330. py-block-comment-prefix at the start of each line.  If given a prefix
  11331. argument (like `\\[universal-argument]', which is kinda mnemonic for `uncomment'), it instead
  11332. deletes that string from the start of each line in the region.
  11333.  
  11334. MOVING POINT AROUND
  11335.  
  11336. \\[py-previous-statement]\t move to statement preceding point
  11337. \\[py-next-statement]\t move to statement following point
  11338. \\[py-goto-block-up]\t move up to start of current block
  11339.  
  11340. The first two move to one statement beyond the statement that contains
  11341. point.  A numeric prefix argument tells them to move that many
  11342. statements instead.  Blank lines, comment lines, and continuation lines
  11343. do not count as `statements' for these commands.  So, e.g., you can go
  11344. to the first code statement in a file by entering
  11345. \t\\[beginning-of-buffer]\t to move to the top of the file
  11346. \t\\[py-next-statement]\t to skip over initial comments and blank lines
  11347. Or do `\\[py-previous-statement]' with a huge prefix argument.
  11348.  
  11349. `\\[py-goto-block-up]' goes to the statement that starts the smallest enclosing block;
  11350. roughly speaking, this will be the closest preceding statement that ends
  11351. with a colon and is indented less than the statement you started on.
  11352. Also sets the mark to the starting point.
  11353.  
  11354. SOME LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
  11355.  
  11356. `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
  11357.  
  11358. `\\[set-selective-display]' with a `small' prefix arg is ideally suited for seeing
  11359. the overall class and def structure of a module.
  11360.  
  11361. PYTHON-MODE's LOCAL KEYMAP
  11362. \\{py-mode-map}"
  11363.  
  11364.   (interactive)
  11365.   (kill-all-local-variables)
  11366.   (setq  major-mode 'python-mode  mode-name "Python")
  11367.   (use-local-map py-mode-map)
  11368.   (set-syntax-table py-mode-syntax-table)
  11369.  
  11370.   (mapcar (function (lambda (x)
  11371.               (make-local-variable (car x))
  11372.               (set (car x) (cdr x))))
  11373.       '( (paragraph-separate . "^[ \t]*$")
  11374.          (paragraph-start     . "^[ \t]*$")
  11375.          (require-final-newline . t)
  11376.          (comment-start .        "# ")
  11377.          (comment-start-skip .    "# *")
  11378.          (comment-column . 40)
  11379.          (indent-line-function . py-indent-line)))
  11380.  
  11381.   ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
  11382.  
  11383.   ;; not sure where the magic comment has to be; to save time searching
  11384.   ;; for a rarity, we give up if it's not found prior to the first
  11385.   ;; executable statement
  11386.   (let ( (case-fold-search nil)
  11387.      new-tab-width)
  11388.     (if (re-search-forward
  11389.      "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
  11390.      (prog2 (py-next-statement 1) (point) (goto-char 1))
  11391.      t)
  11392.     (progn
  11393.       (setq new-tab-width
  11394.         (string-to-int
  11395.          (buffer-substring (match-beginning 1) (match-end 1))))
  11396.       (if (= tab-width new-tab-width)
  11397.           nil
  11398.         (setq tab-width new-tab-width)
  11399.         (message "Caution: tab-width changed to %d" new-tab-width)
  11400.         (if py-beep-if-tab-change (beep))))))
  11401.  
  11402.   (run-hooks 'py-mode-hook))
  11403.  
  11404. ;;; Functions that execute Python commands in a subprocess
  11405.  
  11406. (defun py-shell ()
  11407.   "Start an interactive Python interpreter in another window.
  11408. The variable py-python-command names the interpreter."
  11409.   (interactive)
  11410.   (require 'shell)
  11411.   (switch-to-buffer-other-window
  11412.    (make-shell "Python" py-python-command))
  11413.   (make-local-variable 'shell-prompt-pattern)
  11414.   (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. "))
  11415.  
  11416. (defun py-execute-region (start end)
  11417.   "Send the region between START and END to a Python interpreter.
  11418. If there is a *Python* process it is used."
  11419.   (interactive "r")
  11420.   (condition-case nil
  11421.       (process-send-string "Python" (buffer-substring start end))
  11422.     (error (shell-command-on-region start end py-python-command nil))))
  11423.  
  11424. (defun py-execute-buffer nil
  11425.   "Send the contents of the buffer to a Python interpreter.
  11426. If there is a *Python* process buffer it is used."
  11427.   (interactive)
  11428.   (py-execute-region (point-min) (point-max)))
  11429.  
  11430.  
  11431. ;;; Functions for Python style indentation
  11432.  
  11433. (defun py-delete-char ()
  11434.   "Reduce indentation or delete character.
  11435. If at first non-blank character, or at end of blank line, reduce
  11436. indentation by py-indent-offset columns.  Else delete preceding
  11437. character, converting tabs to spaces."
  11438.   (interactive)
  11439.   (backward-delete-char-untabify
  11440.    (if (and
  11441.     (= (current-indentation) (current-column))
  11442.     (>= (current-indentation) py-indent-offset))
  11443.        py-indent-offset
  11444.      1)))
  11445.  
  11446. (defun py-indent-line ()
  11447.   "Fix the indentation of the current line according to Python rules."
  11448.   (interactive)
  11449.   (let* ( (ci (current-indentation))
  11450.       (move-to-indentation-p (<= (current-column) ci))
  11451.       (need (py-compute-indentation)) )
  11452.     (if (/= ci need)
  11453.     (save-excursion
  11454.       (beginning-of-line)
  11455.       (delete-horizontal-space)
  11456.       (indent-to need)))
  11457.     (if move-to-indentation-p (back-to-indentation))))
  11458.  
  11459. (defun py-newline-and-indent ()
  11460.   "Strives to act like the Emacs newline-and-indent.
  11461. This is just `strives to' because correct indentation can't be computed
  11462. from scratch for Python code.  In general, deletes the whitespace before
  11463. point, inserts a newline, and takes an educated guess as to how you want
  11464. the new line indented."
  11465.   (interactive)
  11466.   (let ( (ci (current-indentation)) )
  11467.     (if (or (< ci (current-column))    ; if point is beyond indentation
  11468.         (looking-at "[ \t]*$"))    ; or line is empty
  11469.     (newline-and-indent)
  11470.       ;; else try to act like newline-and-indent "normally" acts
  11471.       (beginning-of-line)
  11472.       (insert-char ?\n 1)
  11473.       (move-to-column ci))))
  11474.  
  11475. (defun py-compute-indentation ()
  11476.   (save-excursion
  11477.     (beginning-of-line)
  11478.     (cond
  11479.      ;; are we on a continuation line?
  11480.      ( (py-continuation-line-p)
  11481.        (forward-line -1)
  11482.        (if (py-continuation-line-p) ; on at least 3rd line in block
  11483.        (current-indentation)    ; so just continue the pattern
  11484.      ;; else on 2nd line in block, so indent more
  11485.      (+ (current-indentation) py-indent-offset
  11486.         py-continuation-offset)))
  11487.      ;; not on a continuation line
  11488.  
  11489.      ;; if at start of restriction, or on a comment line, assume they
  11490.      ;; intended whatever's there
  11491.      ( (or (bobp) (looking-at "[ \t]*#"))
  11492.        (current-indentation) )
  11493.  
  11494.      ;; else indentation based on that of the statement that precedes
  11495.      ;; us; use the first line of that statement to establish the base,
  11496.      ;; in case the user forced a non-std indentation for the
  11497.      ;; continuation lines (if any)
  11498.      ( t
  11499.        ;; skip back over blank & non-indenting comment lines
  11500.        ;; note:  will skip a blank or non-indenting comment line that
  11501.        ;; happens to be a continuation line too
  11502.        (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
  11503.                nil 'move)
  11504.        (py-goto-initial-line)
  11505.        (if (looking-at py-colon-line-re)
  11506.        (+ (current-indentation) py-indent-offset)
  11507.      (current-indentation))))))
  11508.  
  11509. (defun py-shift-region (start end count)
  11510.   (save-excursion
  11511.     (goto-char end)   (beginning-of-line) (setq end (point))
  11512.     (goto-char start) (beginning-of-line) (setq start (point))
  11513.     (indent-rigidly start end count)))
  11514.  
  11515. (defun py-shift-region-left (start end &optional count)
  11516.   "Shift region of Python code to the left.
  11517. The lines from the line containing the start of the current region up
  11518. to (but not including) the line containing the end of the region are
  11519. shifted to the left, by py-indent-offset columns.
  11520.  
  11521. If a prefix argument is given, the region is instead shifted by that
  11522. many columns."
  11523.   (interactive "*r\nP")   ; region; raw prefix arg
  11524.   (py-shift-region start end
  11525.            (- (prefix-numeric-value
  11526.                (or count py-indent-offset)))))
  11527.  
  11528. (defun py-shift-region-right (start end &optional count)
  11529.   "Shift region of Python code to the right.
  11530. The lines from the line containing the start of the current region up
  11531. to (but not including) the line containing the end of the region are
  11532. shifted to the right, by py-indent-offset columns.
  11533.  
  11534. If a prefix argument is given, the region is instead shifted by that
  11535. many columns."
  11536.   (interactive "*r\nP")   ; region; raw prefix arg
  11537.   (py-shift-region start end (prefix-numeric-value
  11538.                   (or count py-indent-offset))))
  11539.  
  11540. (defun py-indent-region (start end &optional indent-offset)
  11541.   "Reindent a region of Python code.
  11542. The lines from the line containing the start of the current region up
  11543. to (but not including) the line containing the end of the region are
  11544. reindented.  If the first line of the region has a non-whitespace
  11545. character in the first column, the first line is left alone and the rest
  11546. of the region is reindented with respect to it.  Else the entire region
  11547. is reindented with respect to the (closest code or indenting-comment)
  11548. statement immediately preceding the region.
  11549.  
  11550. This is useful when code blocks are moved or yanked, when enclosing
  11551. control structures are introduced or removed, or to reformat code using
  11552. a new value for the indentation offset.
  11553.  
  11554. If a numeric prefix argument is given, it will be used as the value of
  11555. the indentation offset.  Else the value of py-indent-offset will be
  11556. used.
  11557.  
  11558. Warning:  The region must be consistently indented before this function
  11559. is called!  This function does not compute proper indentation from
  11560. scratch (that's impossible in Python), it merely adjusts the existing
  11561. indentation to be correct in context.
  11562.  
  11563. Warning:  This function really has no idea what to do with non-indenting
  11564. comment lines, and shifts them as if they were indenting comment lines.
  11565. Fixing this appears to require telepathy.
  11566.  
  11567. Special cases:  whitespace is deleted from blank lines; continuation
  11568. lines are shifted by the same amount their initial line was shifted, in
  11569. order to preserve their relative indentation with respect to their
  11570. initial line; and comment lines beginning in column 1 are ignored."
  11571.  
  11572.   (interactive "*r\nP") ; region; raw prefix arg
  11573.   (save-excursion
  11574.     (goto-char end)   (beginning-of-line) (setq end (point-marker))
  11575.     (goto-char start) (beginning-of-line)
  11576.     (let ( (py-indent-offset (prefix-numeric-value
  11577.                   (or indent-offset py-indent-offset)))
  11578.        (indents '(-1))    ; stack of active indent levels
  11579.        (target-column 0)    ; column to which to indent
  11580.        (base-shifted-by 0)    ; amount last base line was shifted
  11581.        (indent-base (if (looking-at "[ \t\n]")
  11582.                 (py-compute-indentation)
  11583.               0))
  11584.        ci)
  11585.       (while (< (point) end)
  11586.     (setq ci (current-indentation))
  11587.     ;; figure out appropriate target column
  11588.     (cond
  11589.      ( (or (eq (following-char) ?#)    ; comment in column 1
  11590.            (looking-at "[ \t]*$"))    ; entirely blank
  11591.        (setq target-column 0))
  11592.      ( (py-continuation-line-p)    ; shift relative to base line
  11593.        (setq target-column (+ ci base-shifted-by)))
  11594.      (t                ; new base line
  11595.       (if (> ci (car indents))    ; going deeper; push it
  11596.           (setq indents (cons ci indents))
  11597.         ;; else we should have seen this indent before
  11598.         (setq indents (memq ci indents)) ; pop deeper indents
  11599.         (if (null indents)
  11600.         (error "Bad indentation in region, at line %d"
  11601.                (save-restriction
  11602.              (widen)
  11603.              (1+ (count-lines 1 (point)))))))
  11604.       (setq target-column (+ indent-base
  11605.                  (* py-indent-offset
  11606.                     (- (length indents) 2))))
  11607.       (setq base-shifted-by (- target-column ci))))
  11608.     ;; shift as needed
  11609.     (if (/= ci target-column)
  11610.         (progn
  11611.           (delete-horizontal-space)
  11612.           (indent-to target-column)))
  11613.     (forward-line 1))))
  11614.   (set-marker end nil))
  11615.  
  11616. ;;; Functions for moving point
  11617.  
  11618. (defun py-previous-statement (count)
  11619.   "Go to the start of previous Python statement.
  11620. If the statement at point is the i'th Python statement, goes to the
  11621. start of statement i-COUNT.  If there is no such statement, goes to the
  11622. first statement.  Returns count of statements left to move.
  11623. `Statements' do not include blank, comment, or continuation lines."
  11624.   (interactive "p") ; numeric prefix arg
  11625.   (if (< count 0) (py-next-statement (- count))
  11626.     (py-goto-initial-line)
  11627.     (let ( start )
  11628.       (while (and
  11629.           (setq start (point)) ; always true -- side effect
  11630.           (> count 0)
  11631.           (zerop (forward-line -1))
  11632.           (py-goto-statement-at-or-above))
  11633.     (setq count (1- count)))
  11634.       (if (> count 0) (goto-char start)))
  11635.     count))
  11636.  
  11637. (defun py-next-statement (count)
  11638.   "Go to the start of next Python statement.
  11639. If the statement at point is the i'th Python statement, goes to the
  11640. start of statement i+COUNT.  If there is no such statement, goes to the
  11641. last statement.  Returns count of statements left to move.  `Statements'
  11642. do not include blank, comment, or continuation lines."
  11643.   (interactive "p") ; numeric prefix arg
  11644.   (if (< count 0) (py-previous-statement (- count))
  11645.     (beginning-of-line)
  11646.     (let ( start )
  11647.       (while (and
  11648.           (setq start (point)) ; always true -- side effect
  11649.           (> count 0)
  11650.           (py-goto-statement-below))
  11651.     (setq count (1- count)))
  11652.       (if (> count 0) (goto-char start)))
  11653.     count))
  11654.  
  11655. (defun py-goto-block-up (&optional nomark)
  11656.   "Move up to start of current block.
  11657. Go to the statement that starts the smallest enclosing block; roughly
  11658. speaking, this will be the closest preceding statement that ends with a
  11659. colon and is indented less than the statement you started on.  If
  11660. successful, also sets the mark to the starting point.
  11661.  
  11662. `\\[py-mark-block]' can be used afterward to mark the whole code block, if desired.
  11663.  
  11664. If called from a program, the mark will not be set if optional argument
  11665. NOMARK is not nil."
  11666.   (interactive)
  11667.   (let ( (start (point))
  11668.      (found nil)
  11669.      initial-indent)
  11670.     (py-goto-initial-line)
  11671.     ;; if on blank or non-indenting comment line, use the preceding stmt
  11672.     (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
  11673.     (progn
  11674.       (py-goto-statement-at-or-above)
  11675.       (setq found (looking-at py-colon-line-re))))
  11676.     ;; search back for colon line indented less
  11677.     (setq initial-indent (current-indentation))
  11678.     (while (not (or found (bobp)))
  11679.       (setq found
  11680.         (and
  11681.          (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  11682.          (progn
  11683.            (py-goto-initial-line)
  11684.            (and
  11685.         (< (current-indentation) initial-indent)
  11686.         (looking-at py-colon-line-re))))))
  11687.     (if found
  11688.     (progn
  11689.       (or nomark (push-mark start))
  11690.       (back-to-indentation))
  11691.       (goto-char start)
  11692.       (error "Enclosing block not found"))))
  11693.  
  11694. ;;; Functions for marking regions
  11695.  
  11696. (defun py-mark-block ()
  11697.   "Mark following block of lines.
  11698. Easier to use than explain.  It sets the region to an `interesting'
  11699. block of succeeding lines.  If point is on a blank line, it goes down to
  11700. the next non-blank line.  That will be the start of the region.  The end
  11701. of the region depends on the kind of line at the start:
  11702.  
  11703.  - If a comment, the region will include all succeeding comment lines up
  11704.    to (but not including) the next non-comment line (if any).
  11705.  
  11706.  - If a code line that opens a new block, the region will include all
  11707.    succeeding lines up to (but not including) the next code statement
  11708.    (if any) that's indented no more than the starting line, except that
  11709.    trailing blank and comment lines are excluded.  E.g., if the starting
  11710.    line is a `def' statement, the region will be set to the full
  11711.    function definition, but without any trailing `noise' lines.
  11712.  
  11713.  - Else the region will include all succeeding lines up to (but not
  11714.    including) the next blank line, or code or indenting-comment line
  11715.    indented strictly less than the starting line.  Trailing indenting
  11716.    comment lines are included in this case, but not trailing blank
  11717.    lines.
  11718.  
  11719. A msg identifying the location of the mark is displayed in the echo
  11720. area; or do `\\[exchange-point-and-mark]' to flip down to the end."
  11721.   (interactive)
  11722.   (py-goto-initial-line)
  11723.   ;; skip over blank lines
  11724.   (while (and
  11725.       (looking-at "[ \t]*$")    ; while blank line
  11726.       (not (eobp)))            ; & somewhere to go
  11727.     (forward-line 1))
  11728.   (if (eobp)
  11729.       (error "Hit end of buffer without finding a non-blank stmt"))
  11730.   (let ( (initial-pos (point))
  11731.      (initial-indent (current-indentation))
  11732.      last-pos)            ; position of last stmt in region
  11733.     (cond
  11734.      ;; if comment line, suck up the following comment lines
  11735.      ((looking-at "[ \t]*#")
  11736.       (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
  11737.       (re-search-backward "^[ \t]*#")    ; and back to last comment in block
  11738.       (setq last-pos (point)))
  11739.      ;; else if line opens a block, search for next stmt indented <=
  11740.      ((looking-at py-colon-line-re)
  11741.       (while (and
  11742.           (setq last-pos (point))    ; always true -- side effect
  11743.           (py-goto-statement-below)
  11744.           (> (current-indentation) initial-indent))
  11745.     nil))
  11746.      ;; else plain code line; stop at next blank line, or stmt or
  11747.      ;; indenting comment line indented <
  11748.      (t
  11749.       (while (and
  11750.           (setq last-pos (point))    ; always true -- side effect
  11751.           (or (py-goto-beyond-final-line) t)
  11752.           (not (looking-at "[ \t]*$")) ; stop at blank line
  11753.           (or
  11754.            (>= (current-indentation) initial-indent)
  11755.            (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
  11756.     nil)))
  11757.  
  11758.     ;; skip to end of last stmt
  11759.     (goto-char last-pos)
  11760.     (py-goto-beyond-final-line)
  11761.     ;; set mark & display
  11762.     (push-mark (point) 'no-msg)
  11763.  
  11764.     (forward-line -1)
  11765.     (message "Mark set after: %s" (py-suck-up-leading-text))
  11766.  
  11767.     (goto-char initial-pos)))
  11768.  
  11769. (defun py-comment-region (start end &optional uncomment-p)
  11770.   "Comment out region of code; with prefix arg, uncomment region.
  11771. The lines from the line containing the start of the current region up
  11772. to (but not including) the line containing the end of the region are
  11773. commented out, by inserting the string py-block-comment-prefix at the
  11774. start of each line.  With a prefix arg, removes py-block-comment-prefix
  11775. from the start of each line instead.  py-block-comment-prefix should be
  11776. of the form `#x...', where `x' is some character other than a blank or
  11777. a tab, and `...' is arbitrary.  This is so that the indentation commands
  11778. aren't confused by seeing these comments start in the leftmost column."
  11779.   (interactive "*r\nP")   ; region; raw prefix arg
  11780.   (goto-char end)   (beginning-of-line) (setq end (point))
  11781.   (goto-char start) (beginning-of-line) (setq start (point))
  11782.   (let ( (prefix-len (length py-block-comment-prefix)) )
  11783.     (save-excursion
  11784.       (save-restriction
  11785.     (narrow-to-region start end)
  11786.     (while (not (eobp))
  11787.       (if uncomment-p
  11788.           (and (string= py-block-comment-prefix
  11789.                 (buffer-substring
  11790.                  (point) (+ (point) prefix-len)))
  11791.            (delete-char prefix-len))
  11792.         (insert py-block-comment-prefix))
  11793.       (forward-line 1))))))
  11794.  
  11795. ;;; Helper functions
  11796.  
  11797. ;; go to initial line of current statement; usually this is the
  11798. ;; line we're on, but if we're on the 2nd or following lines of a
  11799. ;; continuation block, we need to go up to the first line of the block
  11800. (defun py-goto-initial-line ()
  11801.   (while (py-continuation-line-p)
  11802.     (forward-line -1))
  11803.   (beginning-of-line))
  11804.  
  11805. ;; go to point right beyond final line of current statement; usually
  11806. ;; this is the start of the next line, but if this is a multi-line
  11807. ;; statement we need to skip over the continuation lines
  11808. (defun py-goto-beyond-final-line ()
  11809.   (forward-line 1)
  11810.   (while (and (py-continuation-line-p)
  11811.           (not (eobp)))
  11812.     (forward-line 1)))
  11813.  
  11814. ;; t iff on continuation line == preceding line ends with backslash
  11815. ;; that's not in a comment
  11816. (defun py-continuation-line-p ()
  11817.   (save-excursion
  11818.     (beginning-of-line)
  11819.     (and
  11820.      ;; use a cheap test first to avoid the regexp if possible
  11821.      ;; use 'eq' because char-after may return nil
  11822.      (eq (char-after (- (point) 2)) ?\\ )
  11823.      (progn
  11824.        (forward-line -1) ; since eq test passed, there is a line above
  11825.        (looking-at py-continued-re)))))
  11826.  
  11827. ;; go to start of first statement (not blank or comment or continuation
  11828. ;; line) at or preceding point
  11829. ;; returns t if there is one, else nil
  11830. (defun py-goto-statement-at-or-above ()
  11831.   (py-goto-initial-line)
  11832.   (if (looking-at py-blank-or-comment-re)
  11833.     ;; skip back over blank & comment lines
  11834.     ;; note:  will skip a blank or comment line that happens to be
  11835.     ;; a continuation line too
  11836.     (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
  11837.         (progn (py-goto-initial-line) t)
  11838.       nil)
  11839.     t))
  11840.  
  11841. ;; go to start of first statement (not blank or comment or continuation
  11842. ;; line) following the statement containing point
  11843. ;; returns t if there is one, else nil
  11844. (defun py-goto-statement-below ()
  11845.   (beginning-of-line)
  11846.   (let ( (start (point)) )
  11847.     (forward-line 1)
  11848.     (while (and
  11849.         (py-continuation-line-p)
  11850.         (not (eobp)))
  11851.       (forward-line 1))
  11852.     (while (and
  11853.         (looking-at py-blank-or-comment-re)
  11854.         (not (eobp)))
  11855.       (forward-line 1))
  11856.     (if (eobp)
  11857.     (progn (goto-char start) nil)
  11858.       t)))
  11859.  
  11860. ;; return string in buffer from start of indentation to end of line;
  11861. ;; prefix "..." if leading whitespace was skipped
  11862. (defun py-suck-up-leading-text ()
  11863.   (save-excursion
  11864.     (back-to-indentation)
  11865.     (concat
  11866.      (if (bolp) "" "...")
  11867.      (buffer-substring (point) (progn (end-of-line) (point))))))
  11868.  
  11869. ;; To do:
  11870. ;; - add a newline when executing buffer ending in partial line
  11871. ;; - suppress prompts when executing regions
  11872. ;; - switch back to previous buffer when starting shell
  11873. ;; - support for ptags
  11874.  
  11875. >>> END OF MSG
  11876. 
  11877. 
  11878. Replied: Wed, 19 Feb 1992 11:10:03 +0100
  11879. Replied: "Tim Peters <tim@ksr.com> +inbox"
  11880. Received: by charon.cwi.nl with SMTP; Wed, 19 Feb 1992 06:02:25 +0100
  11881. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  11882.     (5.61/UUNET-internet-primary) id AA24936; Wed, 19 Feb 92 00:02:19 -0500
  11883. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  11884.     (queueing-rmail) id 000151.17724; Wed, 19 Feb 1992 00:01:51 EST
  11885. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  11886.     id AA19410; Tue, 18 Feb 92 23:25:43 EST
  11887. Received: by kaos.ksr.com (4.0/SMI-3.2)
  11888.     id AA07043; Tue, 18 Feb 92 23:25:41 EST
  11889. Message-Id: <9202190425.AA07043@kaos.ksr.com>
  11890. To: python-list@cwi.nl
  11891. Subject: (Emacs) Python mode, version 1.01
  11892. Date: Tue, 18 Feb 92 23:25:41 EST
  11893. From: Tim Peters <tim@ksr.com>
  11894.  
  11895. Sorry for posting this again so soon, but Guido pushed me into doing
  11896. some (what I think is) very valuable stuff, and you'll like it <grin>.
  11897.  
  11898. Question:  If anyone who is interested in python-mode.el does *not* have,
  11899. or know how to use, the 'patch' program, please let me know.  Context
  11900. diffs are generally much smaller than reposting the whole thing, and I'll
  11901. do that in the future unless somebody can't handle it.  In this
  11902. particular case the diff would be larger than the file, though ...
  11903.  
  11904. New stuff:
  11905.  
  11906.   - DEL is much smarter.  When you use it to reduce indentation, it now
  11907.     tells you which code block you're closing, and reduces the
  11908.     indentation to match that of the opening line of the block (so a
  11909.     non-uniform pattern of indentation increments won't confuse it).
  11910.     And if you use it to reduce indentation on a continuation or non-
  11911.     indenting comment line, it will delete just one column at a time (so
  11912.     you can use DEL and SPACE to adjust the indentation to exactly what
  11913.     you want in a straightforward way).
  11914.  
  11915.   - The mode blurb (C-h m) comes up much (about 20 times) faster now.
  11916.     Alas, this was bought at the cost of taking almost everything out
  11917.     of it <grin/sigh>.
  11918.  
  11919.   - A new&improved (but slow) "long" mode blurb can be gotten via the
  11920.     new `C-c C-h m'.  This is even longer than before, but is (I think)
  11921.     improved in several ways.  E.g., the current values of the mode
  11922.     variables are shown, you can search for the character '@' to move
  11923.     among major sections, and the guts of the "leaf" sections exactly
  11924.     match what you'd get if you did `C-h v' and `C-h f (or k)' one name
  11925.     at a time (before, in a futile effort to save space & time, `C-h m'
  11926.     often failed to tell the whole story).
  11927.  
  11928. Note that this mode is getting fancy enough that byte-compile'ing it has
  11929. a real speed payoff.  You do that by going into the directory containing
  11930. the .el file and entering
  11931.  
  11932.     emacs -batch -f batch-byte-compile python-mode.el
  11933.  
  11934. That will create python-mode.elc, and Emacs will load the .elc version
  11935. first when one exists (i.e., the line above is all you have to do; the
  11936. rest is magic).
  11937.  
  11938. darned-glad-python-creates-.pyc's-all-by-itself-ly y'rs  - tim
  11939.  
  11940. Tim Peters   Kendall Square Research Corp
  11941. tim@ksr.com,         ksr!tim@uunet.uu.net
  11942.  
  11943. Change log:
  11944.  
  11945. Tue Feb 18 22:35:04 1992  tim
  11946. version 1.01
  11947. simplified py-goto-statement-below by calling py-goto-beyond-final-line
  11948. changed py-compute-indentation to treat indenting comment lines
  11949.     exactly like code lines
  11950. forced early exit in py-goto-block-up if starting indent is 0
  11951. rewrote py-delete-char to be a lot smarter
  11952.     caters to non-std indentation (takes from first line in block)
  11953.     displays the 1st line of the block being closed
  11954. updated the docs accordingly
  11955. rewrote the `C-h m' support completely:
  11956.     `C-h m' made very bare but very fast
  11957.     new func py-describe-mode does the slow wordy stuff,
  11958.         bound to C-c C-h m
  11959.     new helper py-dump-help-string
  11960.     changed so vrbl & cmd docs are ripped directly out of the
  11961.         vrbls & cmds in question (so no more will C-h v or C-h f
  11962.         say one thing but C-h m say another)
  11963.     changed so all current key bindings are shown for cmds and so
  11964.         current values are shown for vrbls
  11965.     stuck '@' in front of major sections to aid in skipping around
  11966.         the help file
  11967.     rearranged several parts the way Guido did in his copy
  11968.  
  11969. ;;; Major mode for editing Python programs, version 1.01
  11970. ;; by: Michael A. Guravage
  11971. ;;     Guido van Rossum <guido@cwi.nl>
  11972. ;;     Tim Peters <tim@ksr.com>
  11973. ;;
  11974. ;; Copyright (c) 1992  Tim Peters
  11975. ;;
  11976. ;; This software is provided as-is, without express or implied warranty.
  11977. ;; Permission to use, copy, modify, distribute or sell this software,
  11978. ;; without fee, for any purpose and by any individual or organization, is
  11979. ;; hereby granted, provided that the above copyright notice and this
  11980. ;; paragraph appear in all copies.
  11981. ;;
  11982. ;;
  11983. ;; The following statements, placed in your .emacs file or site-init.el,
  11984. ;; will cause this file to be autoloaded, and python-mode invoked, when
  11985. ;; visiting .py files (assuming the file is in your load-path):
  11986. ;;
  11987. ;;    (autoload 'python-mode "python-mode" "" t)
  11988. ;;    (setq auto-mode-alist
  11989. ;;          (cons '("\\.py$" . python-mode) auto-mode-alist))
  11990.  
  11991. (provide 'python-mode)
  11992.  
  11993. ;;; Constants and variables
  11994.  
  11995. (defvar py-python-command "python"
  11996.   "*Shell command used to start Python interpreter.")
  11997.  
  11998. (defvar py-indent-offset 8        ; argue with Guido <grin>
  11999.   "*Indentation increment.")
  12000.  
  12001. (defvar py-continuation-offset 2
  12002.   "*Indentation (in addition to py-indent-offset) for continued lines.
  12003. The additional indentation given to the first continuation line in a
  12004. multi-line statement.  Each subsequent continuation line in the
  12005. statement inherits its indentation from the line that precedes it, so if
  12006. you don't like the default indentation given to the first continuation
  12007. line, change it to something you do like and Python-mode will
  12008. automatically use that for the remaining continuation lines (or, until
  12009. you change the indentation again).")
  12010.  
  12011. (defvar py-block-comment-prefix "##"
  12012.   "*String used by py-comment-region to comment out a block of code.
  12013. This should follow the convention for non-indenting comment lines so
  12014. that the indentation commands won't get confused (i.e., the string
  12015. should be of the form `#x...' where `x' is not a blank or a tab, and
  12016. `...' is arbitrary).")
  12017.  
  12018. (defvar py-beep-if-tab-change t
  12019.   "*Ring the bell if tab-width is changed.
  12020. If a comment of the form
  12021. \t# vi:set tabsize=<number>:
  12022. is found before the first code line when the file is entered, and
  12023. the current value of (the general Emacs variable) tab-width does not
  12024. equal <number>, tab-width is set to <number>, a message saying so is
  12025. displayed in the echo area, and if py-beep-if-tab-change is non-nil the
  12026. Emacs bell is also rung as a warning.")
  12027.  
  12028. (defvar py-mode-map nil "Keymap used in Python mode buffers.")
  12029. (if py-mode-map
  12030.     ()
  12031.   (setq py-mode-map (make-sparse-keymap))
  12032.  
  12033.   ;; shadow global bindings for newline-and-indent w/ the py- version
  12034.   (mapcar (function (lambda (key)
  12035.               (define-key
  12036.             py-mode-map key 'py-newline-and-indent)))
  12037.    (where-is-internal 'newline-and-indent))
  12038.  
  12039.   (define-key py-mode-map "\C-c\C-c"    'py-execute-buffer)
  12040.   (define-key py-mode-map "\C-c|"    'py-execute-region)
  12041.   (define-key py-mode-map "\C-c!"    'py-shell)
  12042.   (define-key py-mode-map "\177"    'py-delete-char)
  12043.   (define-key py-mode-map "\n"        'py-newline-and-indent)
  12044.   (define-key py-mode-map "\C-c\t"    'py-indent-region)
  12045.   (define-key py-mode-map "\C-c<"    'py-shift-region-left)
  12046.   (define-key py-mode-map "\C-c>"    'py-shift-region-right)
  12047.   (define-key py-mode-map "\C-c\C-n"    'py-next-statement)
  12048.   (define-key py-mode-map "\C-c\C-p"    'py-previous-statement)
  12049.   (define-key py-mode-map "\C-c\C-u"    'py-goto-block-up)
  12050.   (define-key py-mode-map "\C-c\C-b"    'py-mark-block)
  12051.   (define-key py-mode-map "\C-c#"    'py-comment-region)
  12052.   (define-key py-mode-map "\C-c\C-hm"    'py-describe-mode))
  12053.  
  12054. (defvar py-mode-syntax-table nil "Python mode syntax table")
  12055. (if py-mode-syntax-table
  12056.     ()
  12057.   (setq py-mode-syntax-table (make-syntax-table))
  12058.   (set-syntax-table py-mode-syntax-table)
  12059.   (modify-syntax-entry ?\( "()")
  12060.   (modify-syntax-entry ?\) ")(")
  12061.   (modify-syntax-entry ?\[ "(]")
  12062.   (modify-syntax-entry ?\] ")[")
  12063.   (modify-syntax-entry ?\{ "(}")
  12064.   (modify-syntax-entry ?\} "){")
  12065.   (modify-syntax-entry ?\_ "w")
  12066.   (modify-syntax-entry ?\' "\"") ; single quote is string quote
  12067.   (modify-syntax-entry ?\` "$")     ; backquote is open and close paren
  12068.   (modify-syntax-entry ?\# "<")     ; hash starts comment
  12069.   (modify-syntax-entry ?\n ">")) ; newline ends comment
  12070.  
  12071. ;; a statement in Python opens a new block iff it ends with a colon;
  12072. ;; while conceptually trivial, quoted strings, continuation lines, and
  12073. ;; comments make this hard.  E.g., consider the statement
  12074. ;; if \
  12075. ;;      1 \
  12076. ;;      :\
  12077. ;;      \
  12078. ;;      \
  12079. ;;      # comment
  12080. ;; here we define some regexps to help
  12081.  
  12082. (defconst py-stringlit-re "'\\([^'\n\\]\\|\\\\.\\)*'"
  12083.   "regexp matching a Python string literal")
  12084.  
  12085. ;; warning!:  when [^#'\n\\] was written as [^#'\n\\]+ (i.e., with a
  12086. ;; '+' suffix), this appeared to run 100x slower in some bad cases.
  12087. (defconst py-colon-line-re
  12088.   (concat
  12089.     "\\(" "[^#'\n\\]" "\\|" py-stringlit-re "\\|" "\\\\\n" "\\)*"
  12090.     ":"
  12091.     "\\(" "[ \t]\\|\\\\\n" "\\)*"
  12092.     "\\(#.*\\)?" "$")
  12093.   "regexp matching Python statements opening a new block")
  12094.  
  12095. ;; this is tricky because a trailing backslash does not mean
  12096. ;; continuation if it's in a comment
  12097. (defconst py-continued-re
  12098.   (concat
  12099.    "\\(" "[^#'\n\\]" "\\|" py-stringlit-re "\\)*"
  12100.    "\\\\$")
  12101.   "regexp matching Python lines that are continued")
  12102.  
  12103. (defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
  12104.   "regexp matching blank or comment lines")
  12105.  
  12106. ;;; General Functions
  12107.  
  12108. (defun python-mode ()
  12109.   "Major mode for editing Python files.
  12110. Do `\\[py-describe-mode]' for detailed documentation.
  12111. Knows about Python tokens, comments and continuation lines.
  12112. Paragraphs are separated by blank lines only.
  12113.  
  12114. COMMANDS
  12115. \\{py-mode-map}
  12116. VARIABLES
  12117.  
  12118. py-python-command\tshell command to invoke Python interpreter
  12119. py-indent-offset\tindentation increment
  12120. py-continuation-offset\textra indentation given to continuation lines
  12121. py-block-comment-prefix\tcomment string used by py-comment-region
  12122. py-beep-if-tab-change\tring the bell if tab-width is changed"
  12123.   (interactive)
  12124.   (kill-all-local-variables)
  12125.   (setq  major-mode 'python-mode  mode-name "Python")
  12126.   (use-local-map py-mode-map)
  12127.   (set-syntax-table py-mode-syntax-table)
  12128.  
  12129.   (mapcar (function (lambda (x)
  12130.               (make-local-variable (car x))
  12131.               (set (car x) (cdr x))))
  12132.       '( (paragraph-separate . "^[ \t]*$")
  12133.          (paragraph-start     . "^[ \t]*$")
  12134.          (require-final-newline . t)
  12135.          (comment-start .        "# ")
  12136.          (comment-start-skip .    "# *")
  12137.          (comment-column . 40)
  12138.          (indent-line-function . py-indent-line)))
  12139.  
  12140.   ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
  12141.  
  12142.   ;; not sure where the magic comment has to be; to save time searching
  12143.   ;; for a rarity, we give up if it's not found prior to the first
  12144.   ;; executable statement
  12145.   (let ( (case-fold-search nil)
  12146.      new-tab-width)
  12147.     (if (re-search-forward
  12148.      "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
  12149.      (prog2 (py-next-statement 1) (point) (goto-char 1))
  12150.      t)
  12151.     (progn
  12152.       (setq new-tab-width
  12153.         (string-to-int
  12154.          (buffer-substring (match-beginning 1) (match-end 1))))
  12155.       (if (= tab-width new-tab-width)
  12156.           nil
  12157.         (setq tab-width new-tab-width)
  12158.         (message "Caution: tab-width changed to %d" new-tab-width)
  12159.         (if py-beep-if-tab-change (beep))))))
  12160.  
  12161.   (run-hooks 'py-mode-hook))
  12162.  
  12163. ;;; Functions that execute Python commands in a subprocess
  12164.  
  12165. (defun py-shell ()
  12166.   "Start an interactive Python interpreter in another window.
  12167. The variable py-python-command names the interpreter."
  12168.   (interactive)
  12169.   (require 'shell)
  12170.   (switch-to-buffer-other-window
  12171.    (make-shell "Python" py-python-command))
  12172.   (make-local-variable 'shell-prompt-pattern)
  12173.   (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. "))
  12174.  
  12175. (defun py-execute-region (start end)
  12176.   "Send the region between START and END to a Python interpreter.
  12177. If there is a *Python* process it is used."
  12178.   (interactive "r")
  12179.   (condition-case nil
  12180.       (process-send-string "Python" (buffer-substring start end))
  12181.     (error (shell-command-on-region start end py-python-command nil))))
  12182.  
  12183. (defun py-execute-buffer nil
  12184.   "Send the contents of the buffer to a Python interpreter.
  12185. If there is a *Python* process buffer it is used."
  12186.   (interactive)
  12187.   (py-execute-region (point-min) (point-max)))
  12188.  
  12189.  
  12190. ;;; Functions for Python style indentation
  12191.  
  12192. (defun py-delete-char ()
  12193.   "Reduce indentation or delete character.
  12194. If point is at the leftmost column, deletes the preceding newline.
  12195.  
  12196. Else if point is at the leftmost non-blank character of a line that is
  12197. neither a continuation line nor a non-indenting comment line, or if
  12198. point is at the end of a blank line, reduces the indentation to match
  12199. that of the line that opened the current block of code.  The line that
  12200. opened the block is displayed in the echo area to help you keep track of
  12201. where you are.
  12202.  
  12203. Else the preceding character is deleted, converting a tab to spaces if
  12204. needed so that only a single column position is deleted."
  12205.   (interactive "*")
  12206.   (if (or (/= (current-indentation) (current-column))
  12207.       (bolp)
  12208.       (py-continuation-line-p)
  12209.       (looking-at "#[^ \t\n]"))    ; non-indenting #
  12210.       (backward-delete-char-untabify 1)
  12211.     ;; else indent the same as the colon line that opened the block
  12212.  
  12213.     ;; force non-blank so py-goto-block-up doesn't ignore it
  12214.     (insert-char ?* 1)
  12215.     (backward-char)
  12216.     (let ( (base-indent 0)        ; indentation of base line
  12217.        (base-text "")        ; and text of base line
  12218.        (base-found-p nil))
  12219.       (condition-case nil        ; in case no enclosing block
  12220.       (save-excursion
  12221.         (py-goto-block-up 'no-mark)
  12222.         (setq base-indent (current-indentation)
  12223.           base-text   (py-suck-up-leading-text)
  12224.           base-found-p t))
  12225.     (error nil))
  12226.       (delete-char 1)            ; toss the dummy character
  12227.       (delete-horizontal-space)
  12228.       (indent-to base-indent)
  12229.       (if base-found-p
  12230.       (message "Closes block: %s" base-text)))))
  12231.  
  12232. (defun py-indent-line ()
  12233.   "Fix the indentation of the current line according to Python rules."
  12234.   (interactive)
  12235.   (let* ( (ci (current-indentation))
  12236.       (move-to-indentation-p (<= (current-column) ci))
  12237.       (need (py-compute-indentation)) )
  12238.     (if (/= ci need)
  12239.     (save-excursion
  12240.       (beginning-of-line)
  12241.       (delete-horizontal-space)
  12242.       (indent-to need)))
  12243.     (if move-to-indentation-p (back-to-indentation))))
  12244.  
  12245. (defun py-newline-and-indent ()
  12246.   "Strives to act like the Emacs newline-and-indent.
  12247. This is just `strives to' because correct indentation can't be computed
  12248. from scratch for Python code.  In general, deletes the whitespace before
  12249. point, inserts a newline, and takes an educated guess as to how you want
  12250. the new line indented."
  12251.   (interactive)
  12252.   (let ( (ci (current-indentation)) )
  12253.     (if (or (< ci (current-column))    ; if point is beyond indentation
  12254.         (looking-at "[ \t]*$"))    ; or line is empty
  12255.     (newline-and-indent)
  12256.       ;; else try to act like newline-and-indent "normally" acts
  12257.       (beginning-of-line)
  12258.       (insert-char ?\n 1)
  12259.       (move-to-column ci))))
  12260.  
  12261. (defun py-compute-indentation ()
  12262.   (save-excursion
  12263.     (beginning-of-line)
  12264.     (cond
  12265.      ;; are we on a continuation line?
  12266.      ( (py-continuation-line-p)
  12267.        (forward-line -1)
  12268.        (if (py-continuation-line-p) ; on at least 3rd line in block
  12269.        (current-indentation)    ; so just continue the pattern
  12270.      ;; else on 2nd line in block, so indent more
  12271.      (+ (current-indentation) py-indent-offset
  12272.         py-continuation-offset)))
  12273.      ;; not on a continuation line
  12274.  
  12275.      ;; if at start of restriction, or on a non-indenting comment line,
  12276.      ;; assume they intended whatever's there
  12277.      ( (or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
  12278.        (current-indentation) )
  12279.  
  12280.      ;; else indentation based on that of the statement that precedes
  12281.      ;; us; use the first line of that statement to establish the base,
  12282.      ;; in case the user forced a non-std indentation for the
  12283.      ;; continuation lines (if any)
  12284.      ( t
  12285.        ;; skip back over blank & non-indenting comment lines
  12286.        ;; note:  will skip a blank or non-indenting comment line that
  12287.        ;; happens to be a continuation line too
  12288.        (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
  12289.                nil 'move)
  12290.        (py-goto-initial-line)
  12291.        (if (looking-at py-colon-line-re)
  12292.        (+ (current-indentation) py-indent-offset)
  12293.      (current-indentation))))))
  12294.  
  12295. (defun py-shift-region (start end count)
  12296.   (save-excursion
  12297.     (goto-char end)   (beginning-of-line) (setq end (point))
  12298.     (goto-char start) (beginning-of-line) (setq start (point))
  12299.     (indent-rigidly start end count)))
  12300.  
  12301. (defun py-shift-region-left (start end &optional count)
  12302.   "Shift region of Python code to the left.
  12303. The lines from the line containing the start of the current region up
  12304. to (but not including) the line containing the end of the region are
  12305. shifted to the left, by py-indent-offset columns.
  12306.  
  12307. If a prefix argument is given, the region is instead shifted by that
  12308. many columns."
  12309.   (interactive "*r\nP")   ; region; raw prefix arg
  12310.   (py-shift-region start end
  12311.            (- (prefix-numeric-value
  12312.                (or count py-indent-offset)))))
  12313.  
  12314. (defun py-shift-region-right (start end &optional count)
  12315.   "Shift region of Python code to the right.
  12316. The lines from the line containing the start of the current region up
  12317. to (but not including) the line containing the end of the region are
  12318. shifted to the right, by py-indent-offset columns.
  12319.  
  12320. If a prefix argument is given, the region is instead shifted by that
  12321. many columns."
  12322.   (interactive "*r\nP")   ; region; raw prefix arg
  12323.   (py-shift-region start end (prefix-numeric-value
  12324.                   (or count py-indent-offset))))
  12325.  
  12326. (defun py-indent-region (start end &optional indent-offset)
  12327.   "Reindent a region of Python code.
  12328. The lines from the line containing the start of the current region up
  12329. to (but not including) the line containing the end of the region are
  12330. reindented.  If the first line of the region has a non-whitespace
  12331. character in the first column, the first line is left alone and the rest
  12332. of the region is reindented with respect to it.  Else the entire region
  12333. is reindented with respect to the (closest code or indenting-comment)
  12334. statement immediately preceding the region.
  12335.  
  12336. This is useful when code blocks are moved or yanked, when enclosing
  12337. control structures are introduced or removed, or to reformat code using
  12338. a new value for the indentation offset.
  12339.  
  12340. If a numeric prefix argument is given, it will be used as the value of
  12341. the indentation offset.  Else the value of py-indent-offset will be
  12342. used.
  12343.  
  12344. Warning:  The region must be consistently indented before this function
  12345. is called!  This function does not compute proper indentation from
  12346. scratch (that's impossible in Python), it merely adjusts the existing
  12347. indentation to be correct in context.
  12348.  
  12349. Warning:  This function really has no idea what to do with non-indenting
  12350. comment lines, and shifts them as if they were indenting comment lines.
  12351. Fixing this appears to require telepathy.
  12352.  
  12353. Special cases:  whitespace is deleted from blank lines; continuation
  12354. lines are shifted by the same amount their initial line was shifted, in
  12355. order to preserve their relative indentation with respect to their
  12356. initial line; and comment lines beginning in column 1 are ignored."
  12357.  
  12358.   (interactive "*r\nP") ; region; raw prefix arg
  12359.   (save-excursion
  12360.     (goto-char end)   (beginning-of-line) (setq end (point-marker))
  12361.     (goto-char start) (beginning-of-line)
  12362.     (let ( (py-indent-offset (prefix-numeric-value
  12363.                   (or indent-offset py-indent-offset)))
  12364.        (indents '(-1))    ; stack of active indent levels
  12365.        (target-column 0)    ; column to which to indent
  12366.        (base-shifted-by 0)    ; amount last base line was shifted
  12367.        (indent-base (if (looking-at "[ \t\n]")
  12368.                 (py-compute-indentation)
  12369.               0))
  12370.        ci)
  12371.       (while (< (point) end)
  12372.     (setq ci (current-indentation))
  12373.     ;; figure out appropriate target column
  12374.     (cond
  12375.      ( (or (eq (following-char) ?#)    ; comment in column 1
  12376.            (looking-at "[ \t]*$"))    ; entirely blank
  12377.        (setq target-column 0))
  12378.      ( (py-continuation-line-p)    ; shift relative to base line
  12379.        (setq target-column (+ ci base-shifted-by)))
  12380.      (t                ; new base line
  12381.       (if (> ci (car indents))    ; going deeper; push it
  12382.           (setq indents (cons ci indents))
  12383.         ;; else we should have seen this indent before
  12384.         (setq indents (memq ci indents)) ; pop deeper indents
  12385.         (if (null indents)
  12386.         (error "Bad indentation in region, at line %d"
  12387.                (save-restriction
  12388.              (widen)
  12389.              (1+ (count-lines 1 (point)))))))
  12390.       (setq target-column (+ indent-base
  12391.                  (* py-indent-offset
  12392.                     (- (length indents) 2))))
  12393.       (setq base-shifted-by (- target-column ci))))
  12394.     ;; shift as needed
  12395.     (if (/= ci target-column)
  12396.         (progn
  12397.           (delete-horizontal-space)
  12398.           (indent-to target-column)))
  12399.     (forward-line 1))))
  12400.   (set-marker end nil))
  12401.  
  12402. ;;; Functions for moving point
  12403.  
  12404. (defun py-previous-statement (count)
  12405.   "Go to the start of previous Python statement.
  12406. If the statement at point is the i'th Python statement, goes to the
  12407. start of statement i-COUNT.  If there is no such statement, goes to the
  12408. first statement.  Returns count of statements left to move.
  12409. `Statements' do not include blank, comment, or continuation lines."
  12410.   (interactive "p") ; numeric prefix arg
  12411.   (if (< count 0) (py-next-statement (- count))
  12412.     (py-goto-initial-line)
  12413.     (let ( start )
  12414.       (while (and
  12415.           (setq start (point)) ; always true -- side effect
  12416.           (> count 0)
  12417.           (zerop (forward-line -1))
  12418.           (py-goto-statement-at-or-above))
  12419.     (setq count (1- count)))
  12420.       (if (> count 0) (goto-char start)))
  12421.     count))
  12422.  
  12423. (defun py-next-statement (count)
  12424.   "Go to the start of next Python statement.
  12425. If the statement at point is the i'th Python statement, goes to the
  12426. start of statement i+COUNT.  If there is no such statement, goes to the
  12427. last statement.  Returns count of statements left to move.  `Statements'
  12428. do not include blank, comment, or continuation lines."
  12429.   (interactive "p") ; numeric prefix arg
  12430.   (if (< count 0) (py-previous-statement (- count))
  12431.     (beginning-of-line)
  12432.     (let ( start )
  12433.       (while (and
  12434.           (setq start (point)) ; always true -- side effect
  12435.           (> count 0)
  12436.           (py-goto-statement-below))
  12437.     (setq count (1- count)))
  12438.       (if (> count 0) (goto-char start)))
  12439.     count))
  12440.  
  12441. (defun py-goto-block-up (&optional nomark)
  12442.   "Move up to start of current block.
  12443. Go to the statement that starts the smallest enclosing block; roughly
  12444. speaking, this will be the closest preceding statement that ends with a
  12445. colon and is indented less than the statement you started on.  If
  12446. successful, also sets the mark to the starting point.
  12447.  
  12448. `\\[py-mark-block]' can be used afterward to mark the whole code block, if desired.
  12449.  
  12450. If called from a program, the mark will not be set if optional argument
  12451. NOMARK is not nil."
  12452.   (interactive)
  12453.   (let ( (start (point))
  12454.      (found nil)
  12455.      initial-indent)
  12456.     (py-goto-initial-line)
  12457.     ;; if on blank or non-indenting comment line, use the preceding stmt
  12458.     (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
  12459.     (progn
  12460.       (py-goto-statement-at-or-above)
  12461.       (setq found (looking-at py-colon-line-re))))
  12462.     ;; search back for colon line indented less
  12463.     (setq initial-indent (current-indentation))
  12464.     (if (zerop initial-indent)
  12465.     ;; force fast exit
  12466.     (goto-char (point-min)))
  12467.     (while (not (or found (bobp)))
  12468.       (setq found
  12469.         (and
  12470.          (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  12471.          (progn
  12472.            (py-goto-initial-line)
  12473.            (and
  12474.         (< (current-indentation) initial-indent)
  12475.         (looking-at py-colon-line-re))))))
  12476.     (if found
  12477.     (progn
  12478.       (or nomark (push-mark start))
  12479.       (back-to-indentation))
  12480.       (goto-char start)
  12481.       (error "Enclosing block not found"))))
  12482.  
  12483. ;;; Functions for marking regions
  12484.  
  12485. (defun py-mark-block ()
  12486.   "Mark following block of lines.
  12487. Easier to use than explain.  It sets the region to an `interesting'
  12488. block of succeeding lines.  If point is on a blank line, it goes down to
  12489. the next non-blank line.  That will be the start of the region.  The end
  12490. of the region depends on the kind of line at the start:
  12491.  
  12492.  - If a comment, the region will include all succeeding comment lines up
  12493.    to (but not including) the next non-comment line (if any).
  12494.  
  12495.  - If a code line that opens a new block, the region will include all
  12496.    succeeding lines up to (but not including) the next code statement
  12497.    (if any) that's indented no more than the starting line, except that
  12498.    trailing blank and comment lines are excluded.  E.g., if the starting
  12499.    line is a `def' statement, the region will be set to the full
  12500.    function definition, but without any trailing `noise' lines.
  12501.  
  12502.  - Else the region will include all succeeding lines up to (but not
  12503.    including) the next blank line, or code or indenting-comment line
  12504.    indented strictly less than the starting line.  Trailing indenting
  12505.    comment lines are included in this case, but not trailing blank
  12506.    lines.
  12507.  
  12508. A msg identifying the location of the mark is displayed in the echo
  12509. area; or do `\\[exchange-point-and-mark]' to flip down to the end."
  12510.   (interactive)
  12511.   (py-goto-initial-line)
  12512.   ;; skip over blank lines
  12513.   (while (and
  12514.       (looking-at "[ \t]*$")    ; while blank line
  12515.       (not (eobp)))            ; & somewhere to go
  12516.     (forward-line 1))
  12517.   (if (eobp)
  12518.       (error "Hit end of buffer without finding a non-blank stmt"))
  12519.   (let ( (initial-pos (point))
  12520.      (initial-indent (current-indentation))
  12521.      last-pos)            ; position of last stmt in region
  12522.     (cond
  12523.      ;; if comment line, suck up the following comment lines
  12524.      ((looking-at "[ \t]*#")
  12525.       (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
  12526.       (re-search-backward "^[ \t]*#")    ; and back to last comment in block
  12527.       (setq last-pos (point)))
  12528.      ;; else if line opens a block, search for next stmt indented <=
  12529.      ((looking-at py-colon-line-re)
  12530.       (while (and
  12531.           (setq last-pos (point))    ; always true -- side effect
  12532.           (py-goto-statement-below)
  12533.           (> (current-indentation) initial-indent))
  12534.     nil))
  12535.      ;; else plain code line; stop at next blank line, or stmt or
  12536.      ;; indenting comment line indented <
  12537.      (t
  12538.       (while (and
  12539.           (setq last-pos (point))    ; always true -- side effect
  12540.           (or (py-goto-beyond-final-line) t)
  12541.           (not (looking-at "[ \t]*$")) ; stop at blank line
  12542.           (or
  12543.            (>= (current-indentation) initial-indent)
  12544.            (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
  12545.     nil)))
  12546.  
  12547.     ;; skip to end of last stmt
  12548.     (goto-char last-pos)
  12549.     (py-goto-beyond-final-line)
  12550.     ;; set mark & display
  12551.     (push-mark (point) 'no-msg)
  12552.  
  12553.     (forward-line -1)
  12554.     (message "Mark set after: %s" (py-suck-up-leading-text))
  12555.  
  12556.     (goto-char initial-pos)))
  12557.  
  12558. (defun py-comment-region (start end &optional uncomment-p)
  12559.   "Comment out region of code; with prefix arg, uncomment region.
  12560. The lines from the line containing the start of the current region up
  12561. to (but not including) the line containing the end of the region are
  12562. commented out, by inserting the string py-block-comment-prefix at the
  12563. start of each line.  With a prefix arg, removes py-block-comment-prefix
  12564. from the start of each line instead."
  12565.   (interactive "*r\nP")   ; region; raw prefix arg
  12566.   (goto-char end)   (beginning-of-line) (setq end (point))
  12567.   (goto-char start) (beginning-of-line) (setq start (point))
  12568.   (let ( (prefix-len (length py-block-comment-prefix)) )
  12569.     (save-excursion
  12570.       (save-restriction
  12571.     (narrow-to-region start end)
  12572.     (while (not (eobp))
  12573.       (if uncomment-p
  12574.           (and (string= py-block-comment-prefix
  12575.                 (buffer-substring
  12576.                  (point) (+ (point) prefix-len)))
  12577.            (delete-char prefix-len))
  12578.         (insert py-block-comment-prefix))
  12579.       (forward-line 1))))))
  12580.  
  12581. ;;; Documentation functions
  12582.  
  12583. ;; dump the long form of the mode blurb; does the usual doc escapes,
  12584. ;; plus lines of the form ^[vc]:name$ to suck variable & command
  12585. ;; docs out of the right places, along with the keys they're on &
  12586. ;; current values
  12587. (defun py-dump-help-string (str)
  12588.   (with-output-to-temp-buffer "*Help*"
  12589.     (let ( kind
  12590.        funcname func funcdoc
  12591.        (start 0) mstart end
  12592.        keys )
  12593.       (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
  12594.     (setq mstart (match-beginning 0)  end (match-end 0)
  12595.           kind (substring str (match-beginning 1) (match-end 1))
  12596.           funcname (substring str (match-beginning 2) (match-end 2))
  12597.           func (car (read-from-string funcname)))
  12598.     (princ (substitute-command-keys (substring str start mstart)))
  12599.     (if (equal kind "c")
  12600.         (setq funcdoc (documentation func)
  12601.           keys (concat
  12602.             "Key(s): "
  12603.             (mapconcat 'key-description
  12604.                    (where-is-internal func py-mode-map)
  12605.                    ", ")))
  12606.       (setq funcdoc (substitute-command-keys
  12607.              (get func 'variable-documentation))
  12608.         keys (concat
  12609.               "Value: "
  12610.               (prin1-to-string (symbol-value func)))))
  12611.     (princ (format "\n-> %s:\t%s\t%s\n\n"
  12612.                (if (equal kind "c") "Command" "Variable")
  12613.                funcname keys))
  12614.     (princ funcdoc)
  12615.     (princ "\n")
  12616.     (setq start end))
  12617.       (princ (substitute-command-keys (substring str start))))
  12618.     (print-help-return-message)))
  12619.  
  12620. (defun py-describe-mode ()
  12621.   "Dump long form of Python-mode docs."
  12622.   (interactive)
  12623.   (py-dump-help-string "Major mode for editing Python files.
  12624. Knows about Python tokens, comments and continuation lines.
  12625. Paragraphs are separated by blank lines only.
  12626.  
  12627. Major sections below begin with the string `@'; specific function and
  12628. variable docs begin with `->'.
  12629.  
  12630. @EXECUTING PYTHON CODE
  12631.  
  12632. \\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
  12633. \\[py-execute-region]\tsends the current region
  12634. \\[py-shell]\tstarts a Python interpreter window; this will be used by
  12635. \tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
  12636. %c:py-execute-buffer
  12637. %c:py-execute-region
  12638. %c:py-shell
  12639.  
  12640. @VARIABLES
  12641.  
  12642. py-python-command\tshell command to invoke Python interpreter
  12643. py-indent-offset\tindentation increment
  12644. py-continuation-offset\textra indentation given to continuation lines
  12645. py-block-comment-prefix\tcomment string used by `\\[py-comment-region]'
  12646. py-beep-if-tab-change\tring the bell if tab-width is changed
  12647. %v:py-python-command
  12648. %v:py-indent-offset
  12649. %v:py-continuation-offset
  12650. %v:py-block-comment-prefix
  12651. %v:py-beep-if-tab-change
  12652.  
  12653. @KINDS OF LINES
  12654.  
  12655. Each physical line in the file is either a `continuation line' (the
  12656. preceding line ends with a backslash that's not part of a comment) or an
  12657. `initial line' (everything else).
  12658.  
  12659. An initial line is in turn a `blank line' (contains nothing except
  12660. possibly blanks or tabs), a `comment line' (leftmost non-blank character
  12661. is `#'), or a `code line' (everything else).
  12662.  
  12663. Comment Lines
  12664.  
  12665. Although all comment lines are treated alike by Python, Python mode
  12666. recognizes two kinds that act differently with respect to indentation.
  12667.  
  12668. An `indenting comment line' is a comment line with a blank, tab or
  12669. nothing after the initial `#'.  The indentation commands (see below)
  12670. treat these exactly as if they were code lines:  a line following an
  12671. indenting comment line will be indented like the comment line.  All
  12672. other comment lines (those with a non-whitespace character immediately
  12673. following the initial `#') are `non-indenting comment lines', and their
  12674. indentation is ignored by the indentation commands.
  12675.  
  12676. Indenting comment lines are by far the usual case, and should be used
  12677. whenever possible.  Non-indenting comment lines are useful in cases like
  12678. these:
  12679.  
  12680. \ta = b   # a very wordy single-line comment that ends up being
  12681. \t        #... continued onto another line
  12682.  
  12683. \tif a == b:
  12684. ##\t\tprint 'panic!' # old code we've `commented out'
  12685. \t\treturn a
  12686.  
  12687. Since the `#...' and `##' comment lines have a non-whitespace character
  12688. following the initial `#', Python mode ignores them when computing the
  12689. proper indentation for the next line.
  12690.  
  12691. Continuation Lines and Statements
  12692.  
  12693. The Python-mode commands generally work on statements instead of on
  12694. individual lines, where a `statement' is a comment or blank line, or a
  12695. code line and all of its following continuation lines (if any)
  12696. considered as a single logical unit.  The commands in this mode
  12697. generally (when it makes sense) automatically move to the start of the
  12698. statement containing point, even if point happens to be in the middle of
  12699. some continuation line.
  12700.  
  12701. A Bad Idea
  12702.  
  12703. Always put something on the initial line of a multi-line statement
  12704. besides the backslash!  E.g., don't do this:
  12705.  
  12706. \t\\
  12707. \ta = b # what's the indentation of this stmt?
  12708.  
  12709. While that's legal Python, it's silly & would be very expensive for
  12710. Python mode to handle correctly.
  12711.  
  12712. @INDENTATION
  12713.  
  12714. Primarily for entering new code:
  12715. \t\\[indent-for-tab-command]\t indent line appropriately
  12716. \t\\[py-newline-and-indent]\t insert newline, then indent
  12717. \t\\[py-delete-char]\t reduce indentation, or delete single character
  12718.  
  12719. Primarily for reindenting existing code:
  12720. \t\\[py-indent-region]\t reindent region to match its context
  12721. \t\\[py-shift-region-left]\t shift region left by py-indent-offset
  12722. \t\\[py-shift-region-right]\t shift region right by py-indent-offset
  12723.  
  12724. Unlike most programming languages, Python uses indentation, and only
  12725. indentation, to specify block structure.  Hence the indentation supplied
  12726. automatically by Python-mode is just an educated guess:  only you know
  12727. the block structure you intend, so only you can supply correct
  12728. indentation.
  12729.  
  12730. The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
  12731. the indentation of preceding statements.  E.g., assuming
  12732. py-indent-offset is 4, after you enter
  12733. \tif a > 0: \\[py-newline-and-indent]
  12734. the cursor will be moved to the position of the `x':
  12735. \tif a > 0:
  12736. \t    x
  12737. If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
  12738. to
  12739. \tif a > 0:
  12740. \t    c = d
  12741. \t    x
  12742. Python-mode cannot know whether that's what you intended, or whether
  12743. \tif a > 0:
  12744. \t    c = d
  12745. \tx
  12746. was your intent.  In general, Python-mode either reproduces the
  12747. indentation of the (closest code or indenting-comment) preceding
  12748. statement, or adds an extra py-indent-offset blanks if the preceding
  12749. statement has `:' as its last significant (non-whitespace and non-
  12750. comment) character.  If the suggested indentation is too much, use
  12751. \\[py-delete-char] to reduce it.
  12752.  
  12753. Warning:  indent-region should not normally be used!  It calls \\[indent-for-tab-command]
  12754. repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
  12755. structure you intend.
  12756. %c:indent-for-tab-command
  12757. %c:py-newline-and-indent
  12758. %c:py-delete-char
  12759.  
  12760. The remaining `indent' functions apply to a region of Python code.  They
  12761. assume the block structure (equals indentation, in Python) of the region
  12762. is correct, and alter the indentation in various ways while preserving
  12763. the block structure:
  12764. %c:py-indent-region
  12765. %c:py-shift-region-left
  12766. %c:py-shift-region-right
  12767.  
  12768. @MARKING & MANIPULATING REGIONS OF CODE
  12769.  
  12770. \\[py-mark-block]\t mark block of lines
  12771. \\[py-comment-region]\t comment out region of code
  12772. \\[universal-argument] \\[py-comment-region]\t uncomment region of code
  12773. %c:py-mark-block
  12774. %c:py-comment-region
  12775.  
  12776. @MOVING POINT
  12777.  
  12778. \\[py-previous-statement]\t move to statement preceding point
  12779. \\[py-next-statement]\t move to statement following point
  12780. \\[py-goto-block-up]\t move up to start of current block
  12781.  
  12782. The first two move to one statement beyond the statement that contains
  12783. point.  A numeric prefix argument tells them to move that many
  12784. statements instead.  Blank lines, comment lines, and continuation lines
  12785. do not count as `statements' for these commands.  So, e.g., you can go
  12786. to the first code statement in a file by entering
  12787. \t\\[beginning-of-buffer]\t to move to the top of the file
  12788. \t\\[py-next-statement]\t to skip over initial comments and blank lines
  12789. Or do `\\[py-previous-statement]' with a huge prefix argument.
  12790. %c:py-previous-statement
  12791. %c:py-next-statement
  12792. %c:py-goto-block-up
  12793.  
  12794. @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
  12795.  
  12796. `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
  12797.  
  12798. `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing
  12799. the overall class and def structure of a module.
  12800.  
  12801. `\\[indent-relative]' is handy for creating odd indentation.
  12802.  
  12803. @OTHER EMACS HINTS
  12804.  
  12805. If you don't like the default value of a variable, change its value to
  12806. whatever you do like by putting a `setq' line in your .emacs file.
  12807. E.g., to set the indentation increment to 4, put this line in your
  12808. .emacs:
  12809. \t(setq  py-indent-offset  4)
  12810. To see the value of a variable, do `\\[describe-variable]' and enter the variable
  12811. name at the prompt.
  12812.  
  12813. Entering Python mode calls with no arguments the value of the variable
  12814. `py-mode-hook', if that value exists and is not nil; see the `Hooks'
  12815. section of the Elisp manual for details.
  12816.  
  12817. Obscure:  When python-mode is first loaded, it looks for all bindings
  12818. to newline-and-indent in the global keymap, and shadows them with
  12819. local bindings to py-newline-and-indent."))
  12820.  
  12821. ;;; Helper functions
  12822.  
  12823. ;; go to initial line of current statement; usually this is the
  12824. ;; line we're on, but if we're on the 2nd or following lines of a
  12825. ;; continuation block, we need to go up to the first line of the block
  12826. (defun py-goto-initial-line ()
  12827.   (while (py-continuation-line-p)
  12828.     (forward-line -1))
  12829.   (beginning-of-line))
  12830.  
  12831. ;; go to point right beyond final line of current statement; usually
  12832. ;; this is the start of the next line, but if this is a multi-line
  12833. ;; statement we need to skip over the continuation lines
  12834. (defun py-goto-beyond-final-line ()
  12835.   (forward-line 1)
  12836.   (while (and (py-continuation-line-p)
  12837.           (not (eobp)))
  12838.     (forward-line 1)))
  12839.  
  12840. ;; t iff on continuation line == preceding line ends with backslash
  12841. ;; that's not in a comment
  12842. (defun py-continuation-line-p ()
  12843.   (save-excursion
  12844.     (beginning-of-line)
  12845.     (and
  12846.      ;; use a cheap test first to avoid the regexp if possible
  12847.      ;; use 'eq' because char-after may return nil
  12848.      (eq (char-after (- (point) 2)) ?\\ )
  12849.      (progn
  12850.        (forward-line -1) ; since eq test passed, there is a line above
  12851.        (looking-at py-continued-re)))))
  12852.  
  12853. ;; go to start of first statement (not blank or comment or continuation
  12854. ;; line) at or preceding point
  12855. ;; returns t if there is one, else nil
  12856. (defun py-goto-statement-at-or-above ()
  12857.   (py-goto-initial-line)
  12858.   (if (looking-at py-blank-or-comment-re)
  12859.     ;; skip back over blank & comment lines
  12860.     ;; note:  will skip a blank or comment line that happens to be
  12861.     ;; a continuation line too
  12862.     (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
  12863.         (progn (py-goto-initial-line) t)
  12864.       nil)
  12865.     t))
  12866.  
  12867. ;; go to start of first statement (not blank or comment or continuation
  12868. ;; line) following the statement containing point
  12869. ;; returns t if there is one, else nil
  12870. (defun py-goto-statement-below ()
  12871.   (beginning-of-line)
  12872.   (let ( (start (point)) )
  12873.     (py-goto-beyond-final-line)
  12874.     (while (and
  12875.         (looking-at py-blank-or-comment-re)
  12876.         (not (eobp)))
  12877.       (forward-line 1))
  12878.     (if (eobp)
  12879.     (progn (goto-char start) nil)
  12880.       t)))
  12881.  
  12882. ;; return string in buffer from start of indentation to end of line;
  12883. ;; prefix "..." if leading whitespace was skipped
  12884. (defun py-suck-up-leading-text ()
  12885.   (save-excursion
  12886.     (back-to-indentation)
  12887.     (concat
  12888.      (if (bolp) "" "...")
  12889.      (buffer-substring (point) (progn (end-of-line) (point))))))
  12890.  
  12891. ;; To do:
  12892. ;; - add a newline when executing buffer ending in partial line
  12893. ;; - suppress prompts when executing regions
  12894. ;; - switch back to previous buffer when starting shell
  12895. ;; - support for ptags
  12896.  
  12897. >>> END OF MSG
  12898. 
  12899. 
  12900. Received: by charon.cwi.nl with SMTP; Wed, 19 Feb 1992 11:12:55 +0100
  12901. Received: by schelvis.cwi.nl with SMTP; Wed, 19 Feb 1992 10:12:54 GMT
  12902. Message-Id: <9202191012.AA00340@schelvis.cwi.nl>
  12903. To: Tim Peters <tim@ksr.com>
  12904. Cc: python-list@cwi.nl
  12905. Subject: Re: (Emacs) Python mode, version 1.01 
  12906. In-Reply-To: Message by Tim Peters <tim@ksr.com> ,
  12907.          Tue, 18 Feb 92 23:25:41 EST , <9202190425.AA07043@kaos.ksr.com> 
  12908. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  12909. Phone: +31 20 5924098(work), +31 20 6160335(home)
  12910. X-Last-Band-Seen: Henry Rollins Band (Paradiso, 17-2)
  12911. X-Mini-Review: Great!
  12912. Date: Wed, 19 Feb 1992 11:12:53 +0100
  12913. From: Jack Jansen <Jack.Jansen@cwi.nl>
  12914.  
  12915. Tim,
  12916. if you're looking for more things to do to the python mode (grin:-):
  12917. Guido and myself are having eternal battels about indent level: he
  12918. uses 8 and I use 4. Needless to say, neither of us will give up, but I
  12919. often edit code by him (and the other way around). I find that I keep
  12920. using set-variable to change the indent, so I wonder wether it would
  12921. be possible to have a command that more-or-less automatically guesses
  12922. the indent of the current file and sets py-indent-offset accordingly...
  12923. --
  12924. Jack Jansen        | In Holland things are serious, but never hopeless.
  12925. Jack.Jansen@cwi.nl | In Ireland things are hopeless, but never serious.
  12926. uunet!cwi.nl!jack   G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  12927. 
  12928. 
  12929. Received: by charon.cwi.nl with SMTP; Thu, 20 Feb 1992 07:32:36 +0100
  12930. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  12931.     (5.61/UUNET-internet-primary) id AA29378; Thu, 20 Feb 92 01:32:30 -0500
  12932. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  12933.     (queueing-rmail) id 013137.28691; Thu, 20 Feb 1992 01:31:37 EST
  12934. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  12935.     id AA05983; Thu, 20 Feb 92 01:20:29 EST
  12936. Received: by kaos.ksr.com (4.0/SMI-3.2)
  12937.     id AA21214; Thu, 20 Feb 92 01:20:28 EST
  12938. Message-Id: <9202200620.AA21214@kaos.ksr.com>
  12939. To: Jack.Jansen@cwi.nl
  12940. Subject: (Emacs) Python mode, version 1.02
  12941. Cc: python-list@cwi.nl
  12942. Date: Thu, 20 Feb 92 01:20:27 EST
  12943. From: Tim Peters <tim@ksr.com>
  12944.  
  12945. >  [jack jansen]
  12946. >  if you're looking for more things to do to the python mode (grin:-):
  12947.  
  12948. Yes, actually -- of the spare time I've been able to make for Python
  12949. lately, I've spent 95% of it indenting Python instead of actually using
  12950. it <grin/sigh>.  Seriously, I can use all the input from the trenches I
  12951. can get.
  12952.  
  12953. >  Guido and myself are having eternal battles about indent level: he
  12954. >  uses 8 and I use 4.  Needless to say, neither of us will give up,
  12955.  
  12956. Well, of course *you* shouldn't give up, because you're right <grin>.
  12957.  
  12958. >  I often edit code by him (and the other way around). I find that I keep
  12959. >  using set-variable to change the indent, so I wonder wether it would
  12960. >  be possible to have a command that more-or-less automatically guesses
  12961. >  the indent of the current file and sets py-indent-offset accordingly...
  12962.  
  12963. A patch to change python-mode.el from version 1.01 to 1.02 is attached
  12964. below.  The new `C-c :' command should (I think) be close to what you
  12965. want.  Just hit `C-c :'; if it doesn't do what you wanted, read the docs
  12966. & try again.
  12967.  
  12968. Note that, by default, it makes py-indent-offset local to the buffer:
  12969. that way you can have Guido's code in one buffer, yours in another, and
  12970. when moving stuff between them `C-c TAB' will magically reindent
  12971. correctly for the buffer the stuff ends up in.  `C-c C-h m' has also
  12972. been changed to show both the local and the global values of a variable
  12973. (when two versions exist).
  12974.  
  12975. For those feeling nervous, the easiest way to use 'patch' is to
  12976.  
  12977. 1) Read this whole mail msg into a file, say some_file.
  12978.  
  12979. 2) Go to the directory that contains python-mode.el, and enter
  12980.  
  12981.     patch < some_file
  12982.  
  12983.    That will rename the original python-mode.el to python-mode.el.orig,
  12984.    and upgrade python-mode.el to version 1.02.
  12985.  
  12986. a-fellow-indenter-by-4's-ly y'rs  - tim
  12987.  
  12988. Tim Peters   Kendall Square Research Corp
  12989. tim@ksr.com,         ksr!tim@uunet.uu.net
  12990.  
  12991. Change log:
  12992.  
  12993. Thu Feb 20 00:13:34 1992  tim
  12994. version 1.02
  12995. added hint on entering multiple CTRL keys
  12996. added back-to-indentation to the "obscure Emacs cmds" section
  12997. added 'indentation' to the short blurbs saying what py-mode knows about
  12998. tossed progn in py-continuation-line-p (a tad faster)
  12999.     ditto in py-goto-block-up
  13000. changed `x' to `_' in the "why indentation is up to you" blurb
  13001.     for clarity (sez guido; i agree)
  13002. several changes to py-dump-help-string
  13003.     replaced read-from-string by `intern'
  13004.     ditto (princ "\n") -> (terpri)
  13005.     renamed 'kind' -> 'funckind'
  13006.     signal error if funckind unknown
  13007.     display both local & global vrbl values (if appropriate)
  13008. added new function py-guess-indent-offset (a la jack jansen)
  13009.     bound to `C-c :'; updated docs
  13010.  
  13011. Patch:
  13012.  
  13013. *** python-mode.el    Wed Feb 19 23:21:58 1992
  13014. --- python-mode.102.el    Wed Feb 19 23:34:05 1992
  13015. ***************
  13016. *** 1,4 ****
  13017. ! ;;; Major mode for editing Python programs, version 1.01
  13018.   ;; by: Michael A. Guravage
  13019.   ;;     Guido van Rossum <guido@cwi.nl>
  13020.   ;;     Tim Peters <tim@ksr.com>
  13021. --- 1,4 ----
  13022. ! ;;; Major mode for editing Python programs, version 1.02
  13023.   ;; by: Michael A. Guravage
  13024.   ;;     Guido van Rossum <guido@cwi.nl>
  13025.   ;;     Tim Peters <tim@ksr.com>
  13026. ***************
  13027. *** 28,34 ****
  13028.     "*Shell command used to start Python interpreter.")
  13029.   
  13030.   (defvar py-indent-offset 8        ; argue with Guido <grin>
  13031. !   "*Indentation increment.")
  13032.   
  13033.   (defvar py-continuation-offset 2
  13034.     "*Indentation (in addition to py-indent-offset) for continued lines.
  13035. --- 28,36 ----
  13036.     "*Shell command used to start Python interpreter.")
  13037.   
  13038.   (defvar py-indent-offset 8        ; argue with Guido <grin>
  13039. !   "*Indentation increment.
  13040. ! Note that `\\[py-guess-indent-offset]' can usually guess a good value when you're
  13041. ! editing someone else's Python code.")
  13042.   
  13043.   (defvar py-continuation-offset 2
  13044.     "*Indentation (in addition to py-indent-offset) for continued lines.
  13045. ***************
  13046. *** 73,78 ****
  13047. --- 75,81 ----
  13048.     (define-key py-mode-map "\C-c!"    'py-shell)
  13049.     (define-key py-mode-map "\177"    'py-delete-char)
  13050.     (define-key py-mode-map "\n"        'py-newline-and-indent)
  13051. +   (define-key py-mode-map "\C-c:"    'py-guess-indent-offset)
  13052.     (define-key py-mode-map "\C-c\t"    'py-indent-region)
  13053.     (define-key py-mode-map "\C-c<"    'py-shift-region-left)
  13054.     (define-key py-mode-map "\C-c>"    'py-shift-region-right)
  13055. ***************
  13056. *** 140,146 ****
  13057.   (defun python-mode ()
  13058.     "Major mode for editing Python files.
  13059.   Do `\\[py-describe-mode]' for detailed documentation.
  13060. ! Knows about Python tokens, comments and continuation lines.
  13061.   Paragraphs are separated by blank lines only.
  13062.   
  13063.   COMMANDS
  13064. --- 143,149 ----
  13065.   (defun python-mode ()
  13066.     "Major mode for editing Python files.
  13067.   Do `\\[py-describe-mode]' for detailed documentation.
  13068. ! Knows about Python indentation, tokens, comments and continuation lines.
  13069.   Paragraphs are separated by blank lines only.
  13070.   
  13071.   COMMANDS
  13072. ***************
  13073. *** 324,329 ****
  13074. --- 327,387 ----
  13075.          (+ (current-indentation) py-indent-offset)
  13076.        (current-indentation))))))
  13077.   
  13078. + (defun py-guess-indent-offset (&optional global)
  13079. +   "Guess a good value for, and change, py-indent-offset.
  13080. + By default (without a prefix arg), makes a buffer-local copy of
  13081. + py-indent-offset with the new value.  This will not affect any other
  13082. + Python buffers.  With a prefix arg, changes the global value of
  13083. + py-indent-offset.  This affects all Python buffers (that don't have
  13084. + their own buffer-local copy), both those currently existing and those
  13085. + created later in the Emacs session.
  13086. + Some people use a different value for py-indent-offset than you use.
  13087. + There's no excuse for such foolishness, but sometimes you have to deal
  13088. + with their ugly code anyway.  This function examines the file and sets
  13089. + py-indent-offset to what it thinks it was when they created the mess.
  13090. + Specifically, it searches forward from the statement containing point,
  13091. + looking for a line that opens a block of code.  py-indent-offset is set
  13092. + to the difference in indentation between that line and the Python
  13093. + statement following it.  If the search doesn't succeed going forward,
  13094. + it's tried again going backward."
  13095. +   (interactive "P")            ; raw prefix arg
  13096. +   (let ( new-value
  13097. +      (start (point))
  13098. +      (found nil)
  13099. +      colon-indent)
  13100. +     (py-goto-initial-line)
  13101. +     (while (not (or found (eobp)))
  13102. +       (setq found
  13103. +         (and
  13104. +          (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  13105. +          (or (py-goto-initial-line) t) ; always true -- side effect
  13106. +          (looking-at py-colon-line-re))))
  13107. +     (if found
  13108. +     ()
  13109. +       (goto-char start)
  13110. +       (py-goto-initial-line)
  13111. +       (while (not (or found (bobp)))
  13112. +     (setq found
  13113. +           (and
  13114. +            (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  13115. +            (or (py-goto-initial-line) t) ; always true -- side effect
  13116. +            (looking-at py-colon-line-re)))))
  13117. +     (setq colon-indent (current-indentation)
  13118. +       found (and found (zerop (py-next-statement 1)))
  13119. +       new-value (- (current-indentation) colon-indent))
  13120. +     (goto-char start)
  13121. +     (if found
  13122. +     (progn
  13123. +       (funcall (if global 'kill-local-variable 'make-local-variable)
  13124. +            'py-indent-offset)
  13125. +       (setq py-indent-offset new-value)
  13126. +       (message "%s value of py-indent-offset set to %d"
  13127. +            (if global "Global" "Local")
  13128. +            py-indent-offset))
  13129. +       (error "Sorry, couldn't guess a value for py-indent-offset"))))
  13130.   (defun py-shift-region (start end count)
  13131.     (save-excursion
  13132.       (goto-char end)   (beginning-of-line) (setq end (point))
  13133. ***************
  13134. *** 500,510 ****
  13135.         (setq found
  13136.           (and
  13137.            (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  13138. !          (progn
  13139. !            (py-goto-initial-line)
  13140. !            (and
  13141. !         (< (current-indentation) initial-indent)
  13142. !         (looking-at py-colon-line-re))))))
  13143.       (if found
  13144.       (progn
  13145.         (or nomark (push-mark start))
  13146. --- 558,566 ----
  13147.         (setq found
  13148.           (and
  13149.            (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  13150. !          (or (py-goto-initial-line) t) ; always true -- side effect
  13151. !          (< (current-indentation) initial-indent)
  13152. !          (looking-at py-colon-line-re))))
  13153.       (if found
  13154.       (progn
  13155.         (or nomark (push-mark start))
  13156. ***************
  13157. *** 618,650 ****
  13158.   ;; current values
  13159.   (defun py-dump-help-string (str)
  13160.     (with-output-to-temp-buffer "*Help*"
  13161. !     (let ( kind
  13162. !        funcname func funcdoc
  13163.          (start 0) mstart end
  13164.          keys )
  13165.         (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
  13166.       (setq mstart (match-beginning 0)  end (match-end 0)
  13167. !           kind (substring str (match-beginning 1) (match-end 1))
  13168.             funcname (substring str (match-beginning 2) (match-end 2))
  13169. !           func (car (read-from-string funcname)))
  13170.       (princ (substitute-command-keys (substring str start mstart)))
  13171. !     (if (equal kind "c")
  13172. !         (setq funcdoc (documentation func)
  13173. !           keys (concat
  13174. !             "Key(s): "
  13175. !             (mapconcat 'key-description
  13176. !                    (where-is-internal func py-mode-map)
  13177. !                    ", ")))
  13178. !       (setq funcdoc (substitute-command-keys
  13179. !              (get func 'variable-documentation))
  13180. !         keys (concat
  13181. !               "Value: "
  13182. !               (prin1-to-string (symbol-value func)))))
  13183.       (princ (format "\n-> %s:\t%s\t%s\n\n"
  13184. !                (if (equal kind "c") "Command" "Variable")
  13185.                  funcname keys))
  13186.       (princ funcdoc)
  13187. !     (princ "\n")
  13188.       (setq start end))
  13189.         (princ (substitute-command-keys (substring str start))))
  13190.       (print-help-return-message)))
  13191. --- 674,716 ----
  13192.   ;; current values
  13193.   (defun py-dump-help-string (str)
  13194.     (with-output-to-temp-buffer "*Help*"
  13195. !     (let ( (locals (buffer-local-variables))
  13196. !        funckind funcname func funcdoc
  13197.          (start 0) mstart end
  13198.          keys )
  13199.         (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
  13200.       (setq mstart (match-beginning 0)  end (match-end 0)
  13201. !           funckind (substring str (match-beginning 1) (match-end 1))
  13202.             funcname (substring str (match-beginning 2) (match-end 2))
  13203. !           func (intern funcname))
  13204.       (princ (substitute-command-keys (substring str start mstart)))
  13205. !     (cond
  13206. !      ( (equal funckind "c")        ; command
  13207. !        (setq funcdoc (documentation func)
  13208. !          keys (concat
  13209. !                "Key(s): "
  13210. !                (mapconcat 'key-description
  13211. !                   (where-is-internal func py-mode-map)
  13212. !                   ", "))))
  13213. !      ( (equal funckind "v")        ; variable
  13214. !        (setq funcdoc (substitute-command-keys
  13215. !               (get func 'variable-documentation))
  13216. !          keys (if (assq func locals)
  13217. !               (concat
  13218. !                "Local/Global values: "
  13219. !                (prin1-to-string (symbol-value func))
  13220. !                " / "
  13221. !                (prin1-to-string (default-value func)))
  13222. !             (concat
  13223. !              "Value: "
  13224. !              (prin1-to-string (symbol-value func))))))
  13225. !      ( t                ; unexpected
  13226. !        (error "Error in py-dump-help-string, tag `%s'" funckind)))
  13227.       (princ (format "\n-> %s:\t%s\t%s\n\n"
  13228. !                (if (equal funckind "c") "Command" "Variable")
  13229.                  funcname keys))
  13230.       (princ funcdoc)
  13231. !     (terpri)
  13232.       (setq start end))
  13233.         (princ (substitute-command-keys (substring str start))))
  13234.       (print-help-return-message)))
  13235. ***************
  13236. *** 653,659 ****
  13237.     "Dump long form of Python-mode docs."
  13238.     (interactive)
  13239.     (py-dump-help-string "Major mode for editing Python files.
  13240. ! Knows about Python tokens, comments and continuation lines.
  13241.   Paragraphs are separated by blank lines only.
  13242.   
  13243.   Major sections below begin with the string `@'; specific function and
  13244. --- 719,725 ----
  13245.     "Dump long form of Python-mode docs."
  13246.     (interactive)
  13247.     (py-dump-help-string "Major mode for editing Python files.
  13248. ! Knows about Python indentation, tokens, comments and continuation lines.
  13249.   Paragraphs are separated by blank lines only.
  13250.   
  13251.   Major sections below begin with the string `@'; specific function and
  13252. ***************
  13253. *** 749,754 ****
  13254. --- 815,823 ----
  13255.   \t\\[py-delete-char]\t reduce indentation, or delete single character
  13256.   
  13257.   Primarily for reindenting existing code:
  13258. + \t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
  13259. + \t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
  13260.   \t\\[py-indent-region]\t reindent region to match its context
  13261.   \t\\[py-shift-region-left]\t shift region left by py-indent-offset
  13262.   \t\\[py-shift-region-right]\t shift region right by py-indent-offset
  13263. ***************
  13264. *** 763,780 ****
  13265.   the indentation of preceding statements.  E.g., assuming
  13266.   py-indent-offset is 4, after you enter
  13267.   \tif a > 0: \\[py-newline-and-indent]
  13268. ! the cursor will be moved to the position of the `x':
  13269.   \tif a > 0:
  13270. ! \t    x
  13271.   If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
  13272.   to
  13273.   \tif a > 0:
  13274.   \t    c = d
  13275. ! \t    x
  13276.   Python-mode cannot know whether that's what you intended, or whether
  13277.   \tif a > 0:
  13278.   \t    c = d
  13279. ! \tx
  13280.   was your intent.  In general, Python-mode either reproduces the
  13281.   indentation of the (closest code or indenting-comment) preceding
  13282.   statement, or adds an extra py-indent-offset blanks if the preceding
  13283. --- 832,851 ----
  13284.   the indentation of preceding statements.  E.g., assuming
  13285.   py-indent-offset is 4, after you enter
  13286.   \tif a > 0: \\[py-newline-and-indent]
  13287. ! the cursor will be moved to the position of the `_' (_ is not a
  13288. ! character in the file, it's just used here to indicate the location of
  13289. ! the cursor):
  13290.   \tif a > 0:
  13291. ! \t    _
  13292.   If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
  13293.   to
  13294.   \tif a > 0:
  13295.   \t    c = d
  13296. ! \t    _
  13297.   Python-mode cannot know whether that's what you intended, or whether
  13298.   \tif a > 0:
  13299.   \t    c = d
  13300. ! \t_
  13301.   was your intent.  In general, Python-mode either reproduces the
  13302.   indentation of the (closest code or indenting-comment) preceding
  13303.   statement, or adds an extra py-indent-offset blanks if the preceding
  13304. ***************
  13305. *** 789,794 ****
  13306. --- 860,870 ----
  13307.   %c:py-newline-and-indent
  13308.   %c:py-delete-char
  13309.   
  13310. + The next function may be handy when editing code you didn't write:
  13311. + %c:py-guess-indent-offset
  13312.   The remaining `indent' functions apply to a region of Python code.  They
  13313.   assume the block structure (equals indentation, in Python) of the region
  13314.   is correct, and alter the indentation in various ways while preserving
  13315. ***************
  13316. *** 827,835 ****
  13317.   
  13318.   `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
  13319.   
  13320. ! `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing
  13321. ! the overall class and def structure of a module.
  13322.   
  13323.   `\\[indent-relative]' is handy for creating odd indentation.
  13324.   
  13325.   @OTHER EMACS HINTS
  13326. --- 903,913 ----
  13327.   
  13328.   `\\[indent-new-comment-line]' is handy for entering a multi-line comment.
  13329.   
  13330. ! `\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
  13331. ! overall class and def structure of a module.
  13332.   
  13333. + `\\[back-to-indentation]' moves point to a line's first non-blank character.
  13334.   `\\[indent-relative]' is handy for creating odd indentation.
  13335.   
  13336.   @OTHER EMACS HINTS
  13337. ***************
  13338. *** 842,847 ****
  13339. --- 920,931 ----
  13340.   To see the value of a variable, do `\\[describe-variable]' and enter the variable
  13341.   name at the prompt.
  13342.   
  13343. + When entering a key sequence like `C-c C-n', it is not necessary to
  13344. + release the CONTROL key after doing the `C-c' part -- it suffices to
  13345. + press the CONTROL key, press and release `c' (while still holding down
  13346. + CONTROL), press and release `n' (while still holding down CONTROL), &
  13347. + then release CONTROL.
  13348.   Entering Python mode calls with no arguments the value of the variable
  13349.   `py-mode-hook', if that value exists and is not nil; see the `Hooks'
  13350.   section of the Elisp manual for details.
  13351. ***************
  13352. *** 878,886 ****
  13353.        ;; use a cheap test first to avoid the regexp if possible
  13354.        ;; use 'eq' because char-after may return nil
  13355.        (eq (char-after (- (point) 2)) ?\\ )
  13356. !      (progn
  13357. !        (forward-line -1) ; since eq test passed, there is a line above
  13358. !        (looking-at py-continued-re)))))
  13359.   
  13360.   ;; go to start of first statement (not blank or comment or continuation
  13361.   ;; line) at or preceding point
  13362. --- 962,970 ----
  13363.        ;; use a cheap test first to avoid the regexp if possible
  13364.        ;; use 'eq' because char-after may return nil
  13365.        (eq (char-after (- (point) 2)) ?\\ )
  13366. !      ;; make sure; since eq test passed, there is a preceding line
  13367. !      (forward-line -1) ; always true -- side effect
  13368. !      (looking-at py-continued-re))))
  13369.   
  13370.   ;; go to start of first statement (not blank or comment or continuation
  13371.   ;; line) at or preceding point
  13372.  
  13373. >>> END OF MSG
  13374. 
  13375. 
  13376. Received: by charon.cwi.nl with SMTP; Thu, 20 Feb 1992 20:03:09 +0100
  13377. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  13378.     (5.61/UUNET-internet-primary) id AA25663; Thu, 20 Feb 92 14:03:11 -0500
  13379. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  13380.     (queueing-rmail) id 140201.203; Thu, 20 Feb 1992 14:02:01 EST
  13381. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  13382.     id AA11063; Thu, 20 Feb 92 14:01:10 EST
  13383. Received: by kaos.ksr.com (4.0/SMI-3.2)
  13384.     id AA18285; Thu, 20 Feb 92 14:01:08 EST
  13385. Message-Id: <9202201901.AA18285@kaos.ksr.com>
  13386. To: Jack.Jansen@cwi.nl, python-list@cwi.nl
  13387. Subject: (Emacs) Python mode, version 1.02.01
  13388. Date: Thu, 20 Feb 92 14:01:07 EST
  13389. From: Tim Peters <tim@ksr.com>
  13390.  
  13391. The new py-guess-indent-offset (C-c :) could get into an infinite loop
  13392. in the presence of some unusual comment or continuation lines.  The
  13393. patch below fixes that.
  13394.  
  13395. send-me-a-bill-for-the-cycles-you-wasted<grin>-ly y'rs  - tim
  13396.  
  13397. Tim Peters   Kendall Square Research Corp
  13398. tim@ksr.com,         ksr!tim@uunet.uu.net
  13399.  
  13400. Change log:
  13401.  
  13402. Thu Feb 20 13:20:59 1992  tim
  13403. version 1.02.01
  13404. fixed infinite loop in py-guess-indent-offset
  13405.     have to restart re-search-forward where it left off in case
  13406.         colon found at end of, e.g., a comment line
  13407.  
  13408. Patch:
  13409.  
  13410. *** python-mode.el    Thu Feb 20 13:15:17 1992
  13411. --- python-mode.10201.el    Thu Feb 20 13:39:45 1992
  13412. ***************
  13413. *** 1,4 ****
  13414. ! ;;; Major mode for editing Python programs, version 1.02
  13415.   ;; by: Michael A. Guravage
  13416.   ;;     Guido van Rossum <guido@cwi.nl>
  13417.   ;;     Tim Peters <tim@ksr.com>
  13418. --- 1,4 ----
  13419. ! ;;; Major mode for editing Python programs, version 1.02.01
  13420.   ;; by: Michael A. Guravage
  13421.   ;;     Guido van Rossum <guido@cwi.nl>
  13422.   ;;     Tim Peters <tim@ksr.com>
  13423. ***************
  13424. *** 349,363 ****
  13425.     (interactive "P")            ; raw prefix arg
  13426.     (let ( new-value
  13427.        (start (point))
  13428.        (found nil)
  13429.        colon-indent)
  13430.       (py-goto-initial-line)
  13431.       (while (not (or found (eobp)))
  13432. !       (setq found
  13433. !         (and
  13434. !          (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  13435. !          (or (py-goto-initial-line) t) ; always true -- side effect
  13436. !          (looking-at py-colon-line-re))))
  13437.       (if found
  13438.       ()
  13439.         (goto-char start)
  13440. --- 349,366 ----
  13441.     (interactive "P")            ; raw prefix arg
  13442.     (let ( new-value
  13443.        (start (point))
  13444. +      restart
  13445.        (found nil)
  13446.        colon-indent)
  13447.       (py-goto-initial-line)
  13448.       (while (not (or found (eobp)))
  13449. !       (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
  13450. !       (progn
  13451. !         (setq restart (point))
  13452. !         (py-goto-initial-line)
  13453. !         (if (looking-at py-colon-line-re)
  13454. !         (setq found t)
  13455. !           (goto-char restart)))))
  13456.       (if found
  13457.       ()
  13458.         (goto-char start)
  13459.  
  13460. >>> END OF MSG
  13461. 
  13462. 
  13463. Received: by charon.cwi.nl with SMTP; Sat, 22 Feb 1992 06:02:00 +0100
  13464. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  13465.     (5.61/UUNET-internet-primary) id AA01631; Sat, 22 Feb 92 00:02:05 -0500
  13466. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  13467.     (queueing-rmail) id 000121.12599; Sat, 22 Feb 1992 00:01:21 EST
  13468. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  13469.     id AA02584; Fri, 21 Feb 92 23:31:20 EST
  13470. Received: by kaos.ksr.com (4.0/SMI-3.2)
  13471.     id AA16956; Fri, 21 Feb 92 23:31:17 EST
  13472. Message-Id: <9202220431.AA16956@kaos.ksr.com>
  13473. To: guido@cwi.nl
  13474. Subject: (Emacs python-mode) Anyone use `C-c |' or `C-c !'?
  13475. Cc: python-list@cwi.nl
  13476. Date: Fri, 21 Feb 92 23:31:17 EST
  13477. From: Tim Peters <tim@ksr.com>
  13478.  
  13479. I don't have any major problems with py-execute-region or py-execute-
  13480. buffer so long as a Python process doesn't exist.  But if a Python
  13481. process does exist, they don't work for me worth beans if there's more
  13482. than one newline in the region being sent.  Does anyone else have this
  13483. problem?  If so, does anyone know why it happens?  If so, does anyone
  13484. know how to fix it?
  13485.  
  13486. Example:
  13487.  
  13488. 1) Do `C-c !' to start up a Python process.  When I do this, I see a new
  13489.    "*Python*" buffer that looks like:
  13490.  
  13491. Python 0.9.4 alpha (>= Dec 24 1991).
  13492. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  13493. >>>
  13494.  
  13495. 2) In a .py file, set the region around the following three lines:
  13496.  
  13497. a = 9
  13498. b = 12
  13499. print a
  13500.  
  13501. 3) Do `C-c |'.  When I do this, I see the Python process buffer change
  13502.    to:
  13503.  
  13504. Python 0.9.4 alpha (>= Dec 24 1991).
  13505. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  13506. >>> >>>
  13507.  
  13508.    I.e., there's just one additional prompt, and typing stuff in
  13509.    confirms that "a = 9" is the only line Python actually saw.
  13510.  
  13511. In other words, Python only sees (or acts on) the stuff up thru the
  13512. first newline.  This is *not* due to some buffer overflowing in our
  13513. system:  I can send a Python statement containing many thousands of
  13514. characters, and it works fine so long as it's the only statement.  It's
  13515. not really a problem with multiple statements, either -- slamming
  13516. multiple statements into a line works fine so long as they're separated
  13517. by semicolons.  But use newlines instead of semicolons, & only the first
  13518. statement "works".
  13519.  
  13520. I don't think our Emacs is busted, either.  E.g., after
  13521.  
  13522.    (make-shell "Cat" "cat")
  13523.  
  13524. I can process-send-string strings with multiple newlines to the "Cat"
  13525. process and they all get echo'ed.  Ditto to shells, etc.
  13526.  
  13527. In fact, everything I've tried sez that the problem is unique to running
  13528. an interactive Python process.  Guido, does this make any sense to you?
  13529. E.g., in interactive mode, does Python perhaps just ignore stuff after
  13530. the first newline (between printing prompts, that is ...).
  13531.  
  13532. One thing that does work:  breaking the input into lines, & sending them
  13533. to the Python process one at a time, waiting for a prompt after each.
  13534. That's easy enough to do in Elisp, but shudder <grin>.
  13535.  
  13536.  
  13537. An entirely different (i.e., not directly to do with py-mode) problem:
  13538.  
  13539. def fac(n):
  13540.     if n <= 2: return n
  13541.     return n * fac(n-1)
  13542. fac(6)
  13543.  
  13544. That works fine if run as a Python script.  However, if typed in to an
  13545. interactive Python, the "fac(6)" triggers
  13546.  
  13547. fac(6)
  13548.    ^
  13549. Unhandled exception: SyntaxError: invalid syntax
  13550.  
  13551. apparently because in interactive mode Python wants to see an empty line
  13552. terminate the def.  A consequence is that py-execute-region won't always
  13553. work as expected (when a Python process is active) even if the newline
  13554. mystery is cleared up.  This one clearly appears to be a flaw (or
  13555. feature <ahem>) of Python itself.  Agreed?
  13556.  
  13557. at-least-i'm-learning-a-lot-about-elisp<grin>-ly y'rs  - tim
  13558.  
  13559. Tim Peters   Kendall Square Research Corp
  13560. tim@ksr.com,         ksr!tim@uunet.uu.net
  13561. 
  13562. 
  13563. Received: by charon.cwi.nl with SMTP; Sat, 22 Feb 1992 06:32:32 +0100
  13564. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  13565.     (5.61/UUNET-internet-primary) id AA01355; Sat, 22 Feb 92 00:32:13 -0500
  13566. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  13567.     (queueing-rmail) id 003125.16860; Sat, 22 Feb 1992 00:31:25 EST
  13568. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  13569.     id AA03261; Sat, 22 Feb 92 00:04:45 EST
  13570. Received: by kaos.ksr.com (4.0/SMI-3.2)
  13571.     id AA17831; Sat, 22 Feb 92 00:04:44 EST
  13572. Message-Id: <9202220504.AA17831@kaos.ksr.com>
  13573. To: python-list@cwi.nl
  13574. Subject: (Emacs python-mode.el) Patch 1.02.01 -> 1.03
  13575. Date: Sat, 22 Feb 92 00:04:43 EST
  13576. From: Tim Peters <tim@ksr.com>
  13577.  
  13578. Almost all language modes set up bindings for these three keys:
  13579.  
  13580.     ESC C-a        move to start of current function
  13581.     ESC C-e        move to end of current function
  13582.     ESC C-h        wrap region around current function
  13583.  
  13584. The patch below (finally ...) does this for Python.  Two odd wrinkles
  13585. unique to Python:
  13586.  
  13587. 1) "Current function" is ambiguous because it might reasonably be taken
  13588.    to mean "def" or "class".  By default, these keys look at the current
  13589.    def.  Give them a prefix arg (e.g., C-u first), and they look at the
  13590.    current class instead.
  13591.  
  13592. 2) "Current def (or class)" is also ambiguous because def's and class's
  13593.    can nest in arbitrary ways to arbitrary depths in Python.  The
  13594.    ambiguity is resolved by always looking at the smallest def (or
  13595.    class) enclosing point (the first one "up the parse tree").
  13596.  
  13597. Future plans:
  13598.  
  13599. 1) Just about out of both-possible-&-worth-more-than-they-cost ideas for
  13600.    additional editing functions in Python mode.  So speak up if there's
  13601.    something you'd really like that isn't here yet.
  13602.  
  13603. 2) Intend to take a crack at making the "execute Python code" commands
  13604.    pleasant (or at least functional <grin>).
  13605.  
  13606. unbearably y'rs  - tim
  13607.  
  13608. Tim Peters   Kendall Square Research Corp
  13609. tim@ksr.com,         ksr!tim@uunet.uu.net
  13610.  
  13611. Change log:
  13612.  
  13613. Thu Feb 20 22:55:39 1992  tim
  13614. version 1.03
  13615. finally added support for the conventional ESC C-{a,e,h}
  13616.     new helper py-go-up-tree-to-keyword
  13617.     new cmd beginning-of-python-def-or-class, bound to ESC C-a
  13618.     mew cmd end-of-python-def-or-class, bound to ESC C-e
  13619.     new cmd mark-python-def-or-class, bound to ESC C-h
  13620.     added optional arg JUST-MOVE to py-mark-block to help out
  13621.     updated docs
  13622.  
  13623. Patch:
  13624.  
  13625. *** python-mode.el    Fri Feb 21 23:35:59 1992
  13626. --- ../python-mode.el    Fri Feb 21 18:13:10 1992
  13627. ***************
  13628. *** 1,4 ****
  13629. ! ;;; Major mode for editing Python programs, version 1.02.01
  13630.   ;; by: Michael A. Guravage
  13631.   ;;     Guido van Rossum <guido@cwi.nl>
  13632.   ;;     Tim Peters <tim@ksr.com>
  13633. --- 1,4 ----
  13634. ! ;;; Major mode for editing Python programs, version 1.03
  13635.   ;; by: Michael A. Guravage
  13636.   ;;     Guido van Rossum <guido@cwi.nl>
  13637.   ;;     Tim Peters <tim@ksr.com>
  13638. ***************
  13639. *** 84,90 ****
  13640.     (define-key py-mode-map "\C-c\C-u"    'py-goto-block-up)
  13641.     (define-key py-mode-map "\C-c\C-b"    'py-mark-block)
  13642.     (define-key py-mode-map "\C-c#"    'py-comment-region)
  13643. !   (define-key py-mode-map "\C-c\C-hm"    'py-describe-mode))
  13644.   
  13645.   (defvar py-mode-syntax-table nil "Python mode syntax table")
  13646.   (if py-mode-syntax-table
  13647. --- 84,93 ----
  13648.     (define-key py-mode-map "\C-c\C-u"    'py-goto-block-up)
  13649.     (define-key py-mode-map "\C-c\C-b"    'py-mark-block)
  13650.     (define-key py-mode-map "\C-c#"    'py-comment-region)
  13651. !   (define-key py-mode-map "\C-c\C-hm"    'py-describe-mode)
  13652. !   (define-key py-mode-map "\e\C-a"    'beginning-of-python-def-or-class)
  13653. !   (define-key py-mode-map "\e\C-e"    'end-of-python-def-or-class)
  13654. !   (define-key py-mode-map "\e\C-h"    'mark-python-def-or-class))
  13655.   
  13656.   (defvar py-mode-syntax-table nil "Python mode syntax table")
  13657.   (if py-mode-syntax-table
  13658. ***************
  13659. *** 571,579 ****
  13660.         (goto-char start)
  13661.         (error "Enclosing block not found"))))
  13662.   
  13663.   ;;; Functions for marking regions
  13664.   
  13665. ! (defun py-mark-block ()
  13666.     "Mark following block of lines.
  13667.   Easier to use than explain.  It sets the region to an `interesting'
  13668.   block of succeeding lines.  If point is on a blank line, it goes down to
  13669. --- 574,635 ----
  13670.         (goto-char start)
  13671.         (error "Enclosing block not found"))))
  13672.   
  13673. + (defun beginning-of-python-def-or-class (&optional class)
  13674. +   "Move point to start of def (or class, with prefix arg).
  13675. + By default, looks for an appropriate `def'.  If you supply a prefix arg,
  13676. + looks for a `class' instead.  The docs assume the `def' case; just
  13677. + substitute `class' for `def' for the other case:
  13678. + If point is on a blank or non-indenting comment line, moves back to
  13679. + start of closest preceding code statement or indenting comment line.
  13680. + If this is a `def' statement, leaves point at the start of it and
  13681. + returns t.
  13682. + Else searches for the smallest enclosing `def' block, leaves point at
  13683. + the start of it and returns t (note that since class & def statements
  13684. + can nest to arbitrary depths in Python, `smallest enclosing' doesn't
  13685. + necessarily mean `closest preceding that's indented less'; this point is
  13686. + subtle, and this remark is just to let you know that `smallest
  13687. + enclosing' means what it says ...).
  13688. + If no `def' statement can be found by those rules, leaves point at its
  13689. + original location and signals an error.
  13690. + If you just want to mark the def/class, see `\\[mark-python-def-or-class]'."
  13691. +   (interactive "P")            ; raw prefix arg
  13692. +   (let ( (start (point))
  13693. +      (which (if class "class" "def")))
  13694. +     (if (py-go-up-tree-to-keyword which)
  13695. +     t
  13696. +       (goto-char start)
  13697. +       (error "Enclosing %s not found" which))))
  13698. + (defun end-of-python-def-or-class (&optional class)
  13699. +   "Move point beyond end of def (or class, with prefix arg) body.
  13700. + See `\\[beginning-of-python-def-or-class]' docs for how the def (or class) is found.
  13701. + Once the beginning statement is found, this function leaves point
  13702. + immediately after the end of the body of this def (or class).  If it's a
  13703. + one-liner (like `def onemore(n): return n+1'), point will move to the
  13704. + start of the line immediately following the def or class statement.
  13705. + Else point will move beyond the end of the body as defined in the docs
  13706. + for `\\[py-mark-block]'.
  13707. + If you just want to mark the def/class, see `\\[mark-python-def-or-class]'.
  13708. + Returns the position of the start of the def or class."
  13709. +   (interactive "P")            ; raw prefix arg
  13710. +   (beginning-of-python-def-or-class class)
  13711. +   (prog1 (point)
  13712. +     (if (looking-at py-colon-line-re)
  13713. +     (py-mark-block 'just-move)
  13714. +       (py-goto-beyond-final-line))))
  13715.   ;;; Functions for marking regions
  13716.   
  13717. ! (defun py-mark-block (&optional just-move)
  13718.     "Mark following block of lines.
  13719.   Easier to use than explain.  It sets the region to an `interesting'
  13720.   block of succeeding lines.  If point is on a blank line, it goes down to
  13721. ***************
  13722. *** 597,603 ****
  13723.      lines.
  13724.   
  13725.   A msg identifying the location of the mark is displayed in the echo
  13726. ! area; or do `\\[exchange-point-and-mark]' to flip down to the end."
  13727.     (interactive)
  13728.     (py-goto-initial-line)
  13729.     ;; skip over blank lines
  13730. --- 653,663 ----
  13731.      lines.
  13732.   
  13733.   A msg identifying the location of the mark is displayed in the echo
  13734. ! area; or do `\\[exchange-point-and-mark]' to flip down to the end.
  13735. ! If called from a program and optional argument JUST-MOVE is not nil,
  13736. ! instead just moves to the end of the block, and does not set mark or
  13737. ! display a msg."
  13738.     (interactive)
  13739.     (py-goto-initial-line)
  13740.     ;; skip over blank lines
  13741. ***************
  13742. *** 638,651 ****
  13743.       ;; skip to end of last stmt
  13744.       (goto-char last-pos)
  13745.       (py-goto-beyond-final-line)
  13746.       ;; set mark & display
  13747. !     (push-mark (point) 'no-msg)
  13748.   
  13749. !     (forward-line -1)
  13750. !     (message "Mark set after: %s" (py-suck-up-leading-text))
  13751.   
  13752. !     (goto-char initial-pos)))
  13753.   
  13754.   (defun py-comment-region (start end &optional uncomment-p)
  13755.     "Comment out region of code; with prefix arg, uncomment region.
  13756.   The lines from the line containing the start of the current region up
  13757. --- 698,736 ----
  13758.       ;; skip to end of last stmt
  13759.       (goto-char last-pos)
  13760.       (py-goto-beyond-final-line)
  13761.       ;; set mark & display
  13762. !     (if just-move
  13763. !     ()                ; just return
  13764. !       (push-mark (point) 'no-msg)
  13765. !       (forward-line -1)
  13766. !       (message "Mark set after: %s" (py-suck-up-leading-text))
  13767. !       (goto-char initial-pos))))
  13768.   
  13769. ! (defun mark-python-def-or-class (&optional class)
  13770. !   "Set region to body of def (or class, with prefix arg) enclosing point.
  13771. ! Pushes the current mark, then point, on the mark ring (all language
  13772. ! modes do this, but although it's handy it's never documented ...).
  13773.   
  13774. ! See `\\[beginning-of-python-def-or-class]' docs for how the start of the def (or class, with
  13775. ! prefix arg) is found.  This command leaves point in the same place,
  13776. ! except that if the preceding line is blank, point is instead left at its
  13777. ! start (mostly for compatibility with other language modes; it's handy if
  13778. ! you get into the habit of leaving an empty line before def and class
  13779. ! stmts).  The mark is set immediately after the end of the def (or class,
  13780. ! with prefix arg), at the same place `\\[end-of-python-def-or-class]' leaves point."
  13781.   
  13782. +   (interactive "P")            ; raw prefix arg
  13783. +   (push-mark (point))
  13784. +   (let ( (start (end-of-python-def-or-class class)) )
  13785. +     (push-mark (point))
  13786. +     (goto-char (1- start))        ; end of preceding line or bobp
  13787. +     (if (= (current-indentation) (current-column))
  13788. +     (beginning-of-line)
  13789. +       ;; note that (forward-char 1) wouldn't work if def was at
  13790. +       ;; start of restriction
  13791. +       (goto-char start))))
  13792.   (defun py-comment-region (start end &optional uncomment-p)
  13793.     "Comment out region of code; with prefix arg, uncomment region.
  13794.   The lines from the line containing the start of the current region up
  13795. ***************
  13796. *** 879,887 ****
  13797. --- 964,975 ----
  13798.   @MARKING & MANIPULATING REGIONS OF CODE
  13799.   
  13800.   \\[py-mark-block]\t mark block of lines
  13801. + \\[mark-python-def-or-class]\t mark smallest enclosing def
  13802. + \\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
  13803.   \\[py-comment-region]\t comment out region of code
  13804.   \\[universal-argument] \\[py-comment-region]\t uncomment region of code
  13805.   %c:py-mark-block
  13806. + %c:mark-python-def-or-class
  13807.   %c:py-comment-region
  13808.   
  13809.   @MOVING POINT
  13810. ***************
  13811. *** 889,894 ****
  13812. --- 977,986 ----
  13813.   \\[py-previous-statement]\t move to statement preceding point
  13814.   \\[py-next-statement]\t move to statement following point
  13815.   \\[py-goto-block-up]\t move up to start of current block
  13816. + \\[beginning-of-python-def-or-class]\t move to start of def
  13817. + \\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
  13818. + \\[end-of-python-def-or-class]\t move to end of def
  13819. + \\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
  13820.   
  13821.   The first two move to one statement beyond the statement that contains
  13822.   point.  A numeric prefix argument tells them to move that many
  13823. ***************
  13824. *** 901,906 ****
  13825. --- 993,1000 ----
  13826.   %c:py-previous-statement
  13827.   %c:py-next-statement
  13828.   %c:py-goto-block-up
  13829. + %c:beginning-of-python-def-or-class
  13830. + %c:end-of-python-def-or-class
  13831.   
  13832.   @LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
  13833.   
  13834. ***************
  13835. *** 997,1002 ****
  13836. --- 1091,1122 ----
  13837.       (if (eobp)
  13838.       (progn (goto-char start) nil)
  13839.         t)))
  13840. + ;; go to start of statement, at or preceding point, starting with keyword
  13841. + ;; KEY.  Skips blank lines and non-indenting comments upward first.  If
  13842. + ;; that statement starts with KEY, done, else go back to first enclosing
  13843. + ;; block starting with KEY.
  13844. + ;; If successful, leaves point at the start of the KEY line & returns t.
  13845. + ;; Else leaves point at an undefined place & returns nil.
  13846. + (defun py-go-up-tree-to-keyword (key)
  13847. +   ;; skip blanks and non-indenting #
  13848. +   (py-goto-initial-line)
  13849. +   (while (and
  13850. +       (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
  13851. +       (zerop (forward-line -1)))    ; go back
  13852. +     nil)
  13853. +   (py-goto-initial-line)
  13854. +   (let* ( (re (concat "[ \t]*" key "\\b"))
  13855. +       (case-fold-search nil)    ; let* so looking-at sees this
  13856. +       (found (looking-at re))
  13857. +       (dead nil))
  13858. +     (while (not (or found dead))
  13859. +       (condition-case nil        ; in case no enclosing block
  13860. +       (py-goto-block-up 'no-mark)
  13861. +     (error (setq dead t)))
  13862. +       (or dead (setq found (looking-at re))))
  13863. +     (beginning-of-line)
  13864. +     found))
  13865.   
  13866.   ;; return string in buffer from start of indentation to end of line;
  13867.   ;; prefix "..." if leading whitespace was skipped
  13868.  
  13869. >>> END OF MSG
  13870. 
  13871. 
  13872. Replied: Sun, 23 Feb 1992 16:07:52 +0100
  13873. Replied: "spm2d@wilbury.cs.Virginia.EDU +inbox"
  13874. Received: by charon.cwi.nl with SMTP; Sun, 23 Feb 1992 04:00:54 +0100
  13875. Received: from uvacs.cs.virginia.edu by uvaarpa.Virginia.EDU id aa24092;
  13876.           22 Feb 92 22:00 EST
  13877. Received: from wilbury.cs.Virginia.EDU by uvacs.cs.Virginia.EDU (4.1/5.1.UVA)
  13878.     id AA26951; Sat, 22 Feb 92 21:59:34 EST
  13879. Posted-Date: Sat, 22 Feb 92 21:59:02 EST
  13880. Return-Path: <spm2d@wilbury.cs.Virginia.EDU>
  13881. Received: by wilbury.cs.Virginia.EDU (4.1/SMI-2.0)
  13882.     id AA24837; Sat, 22 Feb 92 21:59:02 EST
  13883. Date: Sat, 22 Feb 92 21:59:02 EST
  13884. From: spm2d@wilbury.cs.Virginia.EDU
  13885. Message-Id: <9202230259.AA24837@wilbury.cs.Virginia.EDU>
  13886. To: python-list@cwi.nl
  13887. Subject: getfloatvalue()
  13888.  
  13889. I "fixed" a minor problem with Python and the C extensions. If you
  13890. attempt to read a float and it's actually an int, it doesn't get
  13891. converted; instead, it returns an error. 
  13892.  
  13893. I've fixed it so that it automatically converts it for you. 
  13894. Here is the new version of getfloatdouble(), located in floatobject.c:
  13895.  
  13896. double
  13897. getfloatvalue(op)
  13898.     object *op;
  13899. {
  13900.     if (!is_floatobject(op)) {
  13901.         if(!is_intobject(op)) {
  13902.         err_badarg();
  13903.         return -1;
  13904.         } else return (double)((intobject *)op) -> ob_ival;
  13905.     }
  13906.     else
  13907.         return ((floatobject *)op) -> ob_fval;
  13908. }
  13909.  
  13910. What the old version did was to see if the parameter being passed was
  13911. a float; if not, it returned an error. What I do now is before I
  13912. return, I see if it is an integer, and if it is, I convert it;
  13913. otherwise, there is still an error.
  13914.  
  13915. This could logically be extended to automatically convert strings,
  13916. etc.
  13917.  
  13918. Steve
  13919. 
  13920. 
  13921. Received: by charon.cwi.nl with SMTP; Sun, 23 Feb 1992 21:33:09 +0100
  13922. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  13923.     (5.61/UUNET-internet-primary) id AA27087; Sun, 23 Feb 92 15:33:05 -0500
  13924. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  13925.     (queueing-rmail) id 153120.28884; Sun, 23 Feb 1992 15:31:20 EST
  13926. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  13927.     id AA14100; Sun, 23 Feb 92 15:29:09 EST
  13928. Received: by kaos.ksr.com (4.0/SMI-3.2)
  13929.     id AA00729; Sun, 23 Feb 92 15:29:09 EST
  13930. Message-Id: <9202232029.AA00729@kaos.ksr.com>
  13931. To: guido@cwi.nl
  13932. Subject: Re: (Emacs python-mode) Anyone use `C-c |' or `C-c !'?
  13933. Date: Sun, 23 Feb 92 15:29:08 EST
  13934. From: Tim Peters <tim@ksr.com>
  13935.  
  13936. Hi!
  13937.  
  13938. About the readline library, too complicated for me -- I'm not a UNIX(tm)
  13939. hacker & God willing never will be <0.4 grin>.  I noted too with some
  13940. dismay that the "Input to Processes" section of the Elisp manual sez
  13941.  
  13942. >    Some operating systems have limited space for buffered input in a
  13943. > PTY.  On these systems, the subprocess will cease to read input
  13944. > correctly if you send an input line longer than the system can handle.
  13945. > You cannot avoid the problem by breaking the input into pieces and
  13946. > sending them separately, for the operating system will still have to
  13947. > put all the pieces together in the input buffer before it lets the
  13948. > subprocess read the line.  The only solution is to put the input in a
  13949. > temporary file, and send the process a brief command to read that
  13950. > file.
  13951.  
  13952. That one doesn't seem to be a problem on your or my systems, but
  13953. apparently it's an unsolvable problem on some other systems.  I find it
  13954. hard to imagine that *any* system is too lame to accept one (say) 80-
  13955. character line at a time, though!
  13956.  
  13957. >  >for i in 1,2,3:
  13958. >  >   print i
  13959. >  >a = b
  13960. >
  13961. >  The problem I see here is that if "a = b" were another for statement
  13962. >  ...
  13963.  
  13964. Point taken.  It would be unpleasant for the user (albeit in different
  13965. ways) whether you did or didn't display the first loop's output first --
  13966. can't win!
  13967.  
  13968. >  >for i in 1,2,3: print i
  13969. >  >a = b
  13970. >  >
  13971. >  >Python "should have" known that the for loop was a one-liner ...
  13972. >
  13973. >  It isn't, an 'else' clause can still follow on the next line!
  13974.  
  13975. Oops <turning red and hanging head in shame>.
  13976.  
  13977. >  >This [proceeding after an error] bothers me less than the preceding
  13978. >  >problems. ...
  13979. >
  13980. >  OK, it's a second order problem, but I still don't like it:
  13981.  
  13982. Me neither.
  13983.  
  13984. >  to me, "C-c |" means roughly "try this region".  I want it to fail
  13985. >  as a unit.
  13986.  
  13987. Ya, I share that view; it is subtly different from "send the region to
  13988. the process" in several respects, though.  I should change the docs to
  13989. make that clear.
  13990.  
  13991. >  >Question:  A while back you were considering adding a new Python function
  13992. >  >comparable to the csh "source" (or sh/ksh ".").
  13993. >
  13994. >  You can simulate ". filename" with "exec(open(filename, 'r').read())"
  13995. >  so after all I don't think you need a separate "source" or "execfile"
  13996. >  built-in function after all.
  13997.  
  13998. Last time this came up, I was concerned about the semantics of name-
  13999. resolution inside exec.  I.e., that exec'ing a string doesn't always do
  14000. the same thing as typing the string in.  In the context of code sent
  14001. over by py-mode, though, I *think* the local & global namespaces will
  14002. always be the same, so that it's not a problem (or may be a problem, but
  14003. so subtle that I can't think of how it might fail -- you?).
  14004.  
  14005. But you had another objection to the "execfile" approach, and I think it
  14006. still holds (we were talking about debuggers at the time):
  14007.  
  14008. > [you, in old mail from 13 Jan 92]
  14009. > Sure (there even is a trick to get at the caller's name spaces) but
  14010. > when reading from a file the debugger gets a fair chance of printing
  14011. > source code lines -- the string passed to exec or eval is not saved,
  14012. > but filenames are.
  14013.  
  14014. So here's file tempfile.py:
  14015.  
  14016. 1
  14017. 2
  14018. a = 2 +
  14019. 3
  14020. print 'got here'
  14021.  
  14022. And here's an Emacs Python buffer in action:
  14023.  
  14024. >>> exec(open('tempfile.py','r').read())
  14025. Unhandled exception: SyntaxError: invalid syntax
  14026. Stack backtrace (innermost last):
  14027.   File "<stdin>", line 1
  14028. >>>
  14029.  
  14030. I believe this is an example of the problem you were talking about in
  14031. January, right?  The error msg isn't helpful.
  14032.  
  14033. If instead I'm editing tempfile.py and do a `C-c C-c', the current
  14034. version of py-execute-region (you don't have that yet) yields:
  14035.  
  14036. >>> ## working on region...
  14037. Parsing error: file <stdin>, line 4:
  14038.     a = 2 +
  14039.             ^
  14040. Unhandled exception: SyntaxError: invalid syntax
  14041. >>>
  14042.  
  14043. and an "Unexpected response from Python -- syntax error?" error msg is
  14044. dumped to the echo area.
  14045.  
  14046. The line number is off (because of the inserted "if 1:"), and the
  14047. leading tab on the offending line will be mysterious too, but even so
  14048. it's a much better error msg than we got by exec'ing the file.  So in
  14049. the absence of another approach, I'd rather take a chance on explaining
  14050. the convolutions (the new) py-execute-region undertakes.
  14051.  
  14052. in-a-maze-of-twisty-little-passages-ly y'rs  - tim
  14053. 
  14054. 
  14055. Replied: Tue, 25 Feb 1992 19:00:19 +0100
  14056. Replied: "Tim Peters <tim@ksr.com> +inbox"
  14057. Received: by charon.cwi.nl with SMTP; Mon, 24 Feb 1992 04:32:13 +0100
  14058. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  14059.     (5.61/UUNET-internet-primary) id AA05552; Sun, 23 Feb 92 22:32:13 -0500
  14060. Received: from ksr.UUCP by uunet.uu.net with UUCP/RMAIL
  14061.     (queueing-rmail) id 223130.12213; Sun, 23 Feb 1992 22:31:30 EST
  14062. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  14063.     id AA16642; Sun, 23 Feb 92 22:02:36 EST
  14064. Received: by kaos.ksr.com (4.0/SMI-3.2)
  14065.     id AA03606; Sun, 23 Feb 92 22:02:35 EST
  14066. Message-Id: <9202240302.AA03606@kaos.ksr.com>
  14067. To: python-list@cwi.nl
  14068. Subject: (Emacs python-mode.el) Patch 1.03 -> 1.04
  14069. Date: Sun, 23 Feb 92 22:02:34 EST
  14070. From: Tim Peters <tim@ksr.com>
  14071.  
  14072. If you're curious about why `C-c |' didn't work for me on multi-line
  14073. regions, turns out it worked fine after I recompiled Python without the
  14074. GNU Readline library (& thanks to Guido for suggesting that).  I don't
  14075. know any detail about *why* Readline got in the way, because it's
  14076. irrelevant to the real problem:  there are any number of obscure OS-
  14077. related things that can go wrong in trying to send a long string to a
  14078. process.
  14079.  
  14080. So this patch redoes the `C-c |' and `C-c C-c' commands in a way that
  14081. tries to sidestep all the known problems, both the OS-related headaches
  14082. and the Python's-syntax-is-a-little-different-in-interactive-mode
  14083. headaches.  Seems to work well for me!  Let me know if it doesn't for
  14084. you.
  14085.  
  14086. The major remaining glitches seem too hard to fix without special help
  14087. from Python, and have to do with sending a region to an active Python
  14088. process:
  14089.  
  14090. 1) Because the functions send an altered form of the region to Python,
  14091.    in case of a syntax or runtime error the line numbers reported by
  14092.    Python may not be right, and you may see an extra tab in front of
  14093.    lines that Python echoes.
  14094.  
  14095. 2) In case of a runtime error, in interactive mode Python (apparently)
  14096.    doesn't save the input lines to echo back to you.  This makes sense
  14097.    when you're typing in lines one at a time, but `C-c |' is essentially
  14098.    trying to use an interactive Python in a batch fashion:  if you hit a
  14099.    runtime error while executing a region's code, you won't be shown the
  14100.    line that triggered the error.
  14101.  
  14102. Maybe it would be better overall (it would certainly be less hassle for
  14103. me <grin>) to write the region into a /tmp file and then just send a
  14104. line to Python to tell it to exec the contents of that file.  The reason
  14105. the attached code doesn't settle for that is that the error msgs are
  14106. then even poorer.
  14107.  
  14108. Etc.  Guido & I are arguing about this stuff in the background.  In the
  14109. meantime, I hope you'll find this pleasant enough.
  14110.  
  14111. better-than-what-you-had-anyway-ly y'rs  - tim
  14112.  
  14113. Tim Peters   Kendall Square Research Corp
  14114. tim@ksr.com,         ksr!tim@uunet.uu.net
  14115.  
  14116. Change log:
  14117.  
  14118. Sun Feb 23 18:06:30 1992  tim
  14119. version 1.04
  14120. lots of hacks for execute-code cmds
  14121.     replaced py-execute-region
  14122.     new helper py-process-filter
  14123.     new helper py-process-send-string-wait
  14124.     new helper py-append-to-process-buffer
  14125.     removed to-do's as "done":
  14126. ;; - add a newline when executing buffer ending in partial line
  14127. ;; - suppress prompts when executing regions
  14128.     removed to-do because i like the current behavior better:
  14129. ;; - switch back to previous buffer when starting shell
  14130.     fleshed out docs for py-execute-buffer
  14131.  
  14132. Patch:
  14133.  
  14134. *** python-mode.el    Sun Feb 23 21:18:03 1992
  14135. --- ../python-mode.el    Sun Feb 23 21:01:57 1992
  14136. ***************
  14137. *** 1,4 ****
  14138. ! ;;; Major mode for editing Python programs, version 1.03
  14139.   ;; by: Michael A. Guravage
  14140.   ;;     Guido van Rossum <guido@cwi.nl>
  14141.   ;;     Tim Peters <tim@ksr.com>
  14142. --- 1,4 ----
  14143. ! ;;; Major mode for editing Python programs, version 1.04
  14144.   ;; by: Michael A. Guravage
  14145.   ;;     Guido van Rossum <guido@cwi.nl>
  14146.   ;;     Tim Peters <tim@ksr.com>
  14147. ***************
  14148. *** 212,226 ****
  14149.   
  14150.   (defun py-execute-region (start end)
  14151.     "Send the region between START and END to a Python interpreter.
  14152. ! If there is a *Python* process it is used."
  14153.     (interactive "r")
  14154. !   (condition-case nil
  14155. !       (process-send-string "Python" (buffer-substring start end))
  14156. !     (error (shell-command-on-region start end py-python-command nil))))
  14157.   
  14158. ! (defun py-execute-buffer nil
  14159.     "Send the contents of the buffer to a Python interpreter.
  14160. ! If there is a *Python* process buffer it is used."
  14161.     (interactive)
  14162.     (py-execute-region (point-min) (point-max)))
  14163.   
  14164. --- 212,341 ----
  14165.   
  14166.   (defun py-execute-region (start end)
  14167.     "Send the region between START and END to a Python interpreter.
  14168. ! If there is a *Python* process it is used.  A trailing newline will be
  14169. ! supplied if needed.
  14170. ! If a trailing newline must be supplied, the region is first copied to
  14171. ! buffer `-Python Input-'.
  14172. ! If a *Python* process is used, the process buffer is popped into another
  14173. ! window so you can see it, and the comment
  14174. ! \t## working on region...
  14175. ! is inserted at the end to let you know something is happening.
  14176. ! Following are subtleties to note when using a *Python* process:
  14177. ! The syntax of interactive Python differs a bit from the syntax of
  14178. ! `batch' Python:  in interactive mode, a top-level code block is closed
  14179. ! always and only by an empty line.  So, e.g., in interactive mode this
  14180. ! triggers a syntax error:
  14181. ! for i in 1, 2, 3:
  14182. !    print i,
  14183. !    print i*i
  14184. ! The `for' loop was closed by the empty line, so the indented `print i*i'
  14185. ! makes no sense.
  14186. ! This function worms around such surprises by, in effect, deleting the
  14187. ! blank lines from the region before sending it to Python.  But note that
  14188. ! deleting blank lines can create a (unlikely!) problem of its own:
  14189. ! for i in 1, 2, 3:
  14190. !    print i, \\
  14191. !    print i*i
  14192. ! If someone actually uses entirely blank continuation lines, gripe and
  14193. ! maybe I'll fix it.
  14194. ! Other tricks used to reduce surprises:
  14195. !   - In effect, an `if 1:' is inserted at the front of the region, and a
  14196. !     tab is put in front of every non-blank line in the region.  This
  14197. !     forces Python to treat the region as a unit with respect to
  14198. !     exceptions, so that the first unhandled runtime exception (divide by
  14199. !     0, etc) will abort execution of the region.
  14200. !     Note that in case of error, the line numbers Python reports may be
  14201. !     wrong, because of the `if 1:' inserted at the start and because
  14202. !     Python never sees the blank lines.  Note also that lines displayed
  14203. !     as part of a traceback will have an extra tab at the start.
  14204. !   - The lines are sent to Python one at a time, and the function waits
  14205. !     for Python to respond before sending another.  This is done for two
  14206. !     reasons:  (1) In case of a syntax error, the code detects that Python
  14207. !     is unhappy as soon as the error occurs, and stops sending more
  14208. !     lines.  (2) Sending more than one line at a time simply doesn't work
  14209. !     on many systems, for a variety of obscure reasons."
  14210.     (interactive "r")
  14211. !   (or (< start end) (error "Region is empty"))
  14212. !   (let ( (pyproc (get-process "Python")) )
  14213. !     (save-excursion
  14214. !       ;; if newline not at end, take source from a temp buffer
  14215. !       (cond
  14216. !        ( (= (char-after (1- end)) ?\n)
  14217. !      nil)
  14218. !        ( (and (< end (point-max))
  14219. !           (= (char-after end) ?\n))
  14220. !      (setq end (1+ end)))        ; lucked out
  14221. !        ;; else we have to create a newline
  14222. !        ( t
  14223. !      (let ( (temp-buffer (get-buffer-create "-Python Input-")) )
  14224. !        (copy-to-buffer temp-buffer start end)
  14225. !        (set-buffer temp-buffer)
  14226. !        (goto-char (point-max))
  14227. !        (insert-char ?\n 1)
  14228. !        (setq start (point-min)
  14229. !          end   (point-max)))))
  14230.   
  14231. !       ;; send the text
  14232. !       (if (null pyproc)
  14233. !       (shell-command-on-region start end py-python-command)
  14234. !     ;; else feed it one line at a time, hack upon hack
  14235. !     (unwind-protect
  14236. !         (progn
  14237. !           (set-process-filter pyproc 'py-process-filter)
  14238. !           (py-append-to-process-buffer
  14239. !            pyproc "## working on region...\n")
  14240. !           (py-process-send-string-wait pyproc "if 1:\n")
  14241. !           (goto-char start)
  14242. !           (while (< (point) end)
  14243. !         (if (looking-at "[ \t]*$"); skip blank line
  14244. !             (forward-line 1)
  14245. !           ;; else
  14246. !           (py-process-send-string-wait
  14247. !            pyproc
  14248. !            (concat "\t" (buffer-substring
  14249. !                  (point)
  14250. !                  (progn (forward-line 1) (point)))))))
  14251. !           (set-process-filter pyproc nil)
  14252. !           (process-send-string pyproc "\n"))
  14253. !       ;; unwind-protect'ed code
  14254. !       (set-process-filter pyproc nil))))))
  14255. ! ;; throw away "... " prompts, and gripe about anything else; this is
  14256. ! ;; intended to be used only while py-execute-region is feeding gimmicked
  14257. ! ;; strings to Python
  14258. ! (defun py-process-filter (pyproc string)
  14259. !   ;; (save-excursion
  14260. !   ;;   (set-buffer (get-buffer-create "Trace"))
  14261. !   ;;   (insert "{" string "}"))
  14262. !   (if (equal string "... ")
  14263. !       nil
  14264. !     (py-append-to-process-buffer pyproc string)
  14265. !     (error "Unexpected response from Python -- syntax error?")))
  14266. ! (defun py-execute-buffer ()
  14267.     "Send the contents of the buffer to a Python interpreter.
  14268. ! If there is a *Python* process buffer it is used.  If a clipping
  14269. ! restriction is in effect, only the accessible portion of the buffer is
  14270. ! sent.  A trailing newline will be supplied if needed.
  14271. ! See the `\\[py-execute-region]' docs for an account of some subtleties."
  14272.     (interactive)
  14273.     (py-execute-region (point-min) (point-max)))
  14274.   
  14275. ***************
  14276. *** 1127,1134 ****
  14277.        (if (bolp) "" "...")
  14278.        (buffer-substring (point) (progn (end-of-line) (point))))))
  14279.   
  14280.   ;; To do:
  14281. - ;; - add a newline when executing buffer ending in partial line
  14282. - ;; - suppress prompts when executing regions
  14283. - ;; - switch back to previous buffer when starting shell
  14284.   ;; - support for ptags
  14285. --- 1242,1265 ----
  14286.        (if (bolp) "" "...")
  14287.        (buffer-substring (point) (progn (end-of-line) (point))))))
  14288.   
  14289. + ;; send PROCESS the STRING as input, and wait for the process to send
  14290. + ;; something back
  14291. + (defun py-process-send-string-wait (process string)
  14292. +   (process-send-string process string)
  14293. +   (accept-process-output process))
  14294. + ;; make PROCESS's buffer visible, append STRING to it, and force display
  14295. + (defun py-append-to-process-buffer (process string)
  14296. +   (save-excursion
  14297. +     (let* ( (proc-buf (process-buffer process))
  14298. +         (pop-up-windows t)
  14299. +         (proc-win (display-buffer proc-buf)))
  14300. +       (set-buffer proc-buf)
  14301. +       (goto-char (point-max))
  14302. +       (insert string)
  14303. +       (move-marker (process-mark process) (point-max))
  14304. +       (set-window-point proc-win (point-max))
  14305. +       (sit-for 0))))
  14306.   ;; To do:
  14307.   ;; - support for ptags
  14308.  
  14309. >>> END OF MSG
  14310. 
  14311. 
  14312. Received: by charon.cwi.nl with SMTP; Fri, 28 Feb 1992 05:17:38 +0100
  14313. Received: from ksr.com ([192.9.200.90]) by hopscotch.ksr.com with SMTP
  14314.     id AA16048; Thu, 27 Feb 1992 23:17:21 -0500
  14315. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  14316.     id AA11879; Thu, 27 Feb 92 23:18:30 EST
  14317. Received: by kaos.ksr.com (4.0/KSR-2.0)
  14318.     id AA16305; Thu, 27 Feb 92 23:18:29 EST
  14319. Message-Id: <9202280418.AA16305@kaos.ksr.com>
  14320. To: python-list@cwi.nl
  14321. Subject: (Emacs python-mode.el) Patch for version 1.04 -> 1.05
  14322. Date: Thu, 27 Feb 92 23:18:28 EST
  14323. From: Tim Peters <tim@ksr.com>
  14324.  
  14325. Small changes:
  14326.  
  14327.   - `C-c C-b' now accepts a prefix arg as meaning "mark the following
  14328.     structure".  E.g., if you're on an `if' statement, do `C-u C-c C-b'
  14329.     and the region will be set to the `if' block plus all the following
  14330.     `elif' and `else' blocks (if any) that belong to that `if'
  14331.     structure.  Handy when it's needed, but perhaps that's so rarely
  14332.     that you'll have a hard time remembering how to do it <0.7 grin>.
  14333.  
  14334.     If anyone has written Elisp that calls py-mark-block, note that you
  14335.     must change your calls because the interface has changed (the former
  14336.     only argument is now the second argument; pass `nil' as the first
  14337.     argument and it will do exactly what it did before).
  14338.  
  14339.   - If you ever did `M-x python-mode' by hand, you probably noticed that
  14340.     it (probably) moved point to the start of the buffer as a surprising
  14341.     side effect.  Fixed.
  14342.  
  14343.   - Added some words to the `C-c |' docs:  a handy hint from Guido, and
  14344.     a warning against sending regions to a Python buffer "too fast".
  14345.  
  14346. Plans:  None other than to rewrite the "execute region/buffer" commands
  14347. completely, contingent upon Guido releasing some experimental changes
  14348. he's made to Python itself.  If those make it into the world, sending a
  14349. region or a buffer to an active Python process will change like so:
  14350.  
  14351.   - Error msgs will push the envelope of the cutting edge of the state
  14352.     of the art, a veritable quantum leap beyond the stale & clumsy msgs
  14353.     of today's old-fashioned technology (oops!  forgive me; I'm working
  14354.     at a startup <grin> ...).  The unvarnished truth is that the line
  14355.     numbers will be correct, the naughty lines will actually be
  14356.     displayed, and there won't be tabs in front of them that you didn't
  14357.     put there.
  14358.  
  14359.   - You'll get more display options:  (1) if you want, the process buffer
  14360.     will scroll to show output as it arrives, even if it's not in the
  14361.     selected window; and, (2) if you want, you can remove the process
  14362.     buffer from all the windows and request that it "pop up" again the
  14363.     next time output arrives (handy if you have a long-running
  14364.     computation and don't won't to tie up screen area waiting for it say
  14365.     something).
  14366.  
  14367.   - The two above are certain (already have code that does 'em).  This
  14368.     one is iffy:  it *might* be possible to let you send over regions
  14369.     while other regions are still executing.  This is much harder to do
  14370.     than I thought it would be, even given Guido's changes (without
  14371.     which it, along with the two above, looks next to impossible).
  14372.  
  14373. so-in-the-absence-of-more-bug-reports-version-1.05-looks-like-"it"-
  14374.    for-a-while-ly y'rs  - tim
  14375.  
  14376. Tim Peters   Kendall Square Research Corp
  14377. tim@ksr.com,         ksr!tim@uunet.uu.net
  14378.  
  14379. Change log:
  14380. Thu Feb 27 22:04:27 1992  tim
  14381. version 1.05
  14382. added new arg to py-mark-block to mark whole structures (eg, if/elif/else)
  14383.     added helper py-goto-beyond-block
  14384.         changed end-of-python-def-or-class to use it
  14385.     new helper py-suck-up-first-keyword
  14386.     changed the docs
  14387.     changed other calls to py-mark-block to match new arglist
  14388. added guido's narrow-to-region hint to the py-execute-region docs
  14389.     + warning about waiting for python to finish one region
  14390.         before sending another one
  14391. changed python-mode to restore point when searching for vi tab directive
  14392.     prevents buffer movement when `M-x python-mode' done by hand
  14393.  
  14394. Patch:
  14395.  
  14396. *** python-mode.el    Thu Feb 27 22:36:57 1992
  14397. --- ../python-mode.el    Thu Feb 27 22:17:59 1992
  14398. ***************
  14399. *** 1,4 ****
  14400. ! ;;; Major mode for editing Python programs, version 1.04
  14401.   ;; by: Michael A. Guravage
  14402.   ;;     Guido van Rossum <guido@cwi.nl>
  14403.   ;;     Tim Peters <tim@ksr.com>
  14404. --- 1,4 ----
  14405. ! ;;; Major mode for editing Python programs, version 1.05
  14406.   ;; by: Michael A. Guravage
  14407.   ;;     Guido van Rossum <guido@cwi.nl>
  14408.   ;;     Tim Peters <tim@ksr.com>
  14409. ***************
  14410. *** 181,186 ****
  14411. --- 181,187 ----
  14412.     ;; for a rarity, we give up if it's not found prior to the first
  14413.     ;; executable statement
  14414.     (let ( (case-fold-search nil)
  14415. +      (start (point))
  14416.        new-tab-width)
  14417.       (if (re-search-forward
  14418.        "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
  14419. ***************
  14420. *** 194,200 ****
  14421.             nil
  14422.           (setq tab-width new-tab-width)
  14423.           (message "Caution: tab-width changed to %d" new-tab-width)
  14424. !         (if py-beep-if-tab-change (beep))))))
  14425.   
  14426.     (run-hooks 'py-mode-hook))
  14427.   
  14428. --- 195,202 ----
  14429.             nil
  14430.           (setq tab-width new-tab-width)
  14431.           (message "Caution: tab-width changed to %d" new-tab-width)
  14432. !         (if py-beep-if-tab-change (beep)))))
  14433. !     (goto-char start))
  14434.   
  14435.     (run-hooks 'py-mode-hook))
  14436.   
  14437. ***************
  14438. *** 225,232 ****
  14439. --- 227,244 ----
  14440.   
  14441.   is inserted at the end to let you know something is happening.
  14442.   
  14443. + Hint:  If you want to execute part of a Python file several times (e.g.,
  14444. + perhaps you're developing a function and want to flesh it out a bit at a
  14445. + time), use `\\[narrow-to-region]' to restrict the buffer to the region of interest,
  14446. + and send the code to a *Python* process via `\\[py-execute-buffer]' instead.
  14447.   Following are subtleties to note when using a *Python* process:
  14448.   
  14449. + Related to the following (but in an obscure way), after you send a
  14450. + region to a *Python* process, wait for Python to finish it before
  14451. + sending another region.  Fixing this under the current scheme is
  14452. + difficult, but feel free to try <grin>.
  14453.   The syntax of interactive Python differs a bit from the syntax of
  14454.   `batch' Python:  in interactive mode, a top-level code block is closed
  14455.   always and only by an empty line.  So, e.g., in interactive mode this
  14456. ***************
  14457. *** 737,751 ****
  14458.   Returns the position of the start of the def or class."
  14459.     (interactive "P")            ; raw prefix arg
  14460.     (beginning-of-python-def-or-class class)
  14461. !   (prog1 (point)
  14462. !     (if (looking-at py-colon-line-re)
  14463. !     (py-mark-block 'just-move)
  14464. !       (py-goto-beyond-final-line))))
  14465.   
  14466.   ;;; Functions for marking regions
  14467.   
  14468. ! (defun py-mark-block (&optional just-move)
  14469. !   "Mark following block of lines.
  14470.   Easier to use than explain.  It sets the region to an `interesting'
  14471.   block of succeeding lines.  If point is on a blank line, it goes down to
  14472.   the next non-blank line.  That will be the start of the region.  The end
  14473. --- 749,760 ----
  14474.   Returns the position of the start of the def or class."
  14475.     (interactive "P")            ; raw prefix arg
  14476.     (beginning-of-python-def-or-class class)
  14477. !   (prog1 (point) (py-goto-beyond-block)))
  14478.   
  14479.   ;;; Functions for marking regions
  14480.   
  14481. ! (defun py-mark-block (&optional extend just-move)
  14482. !   "Mark following block of lines.  With prefix arg, mark structure.
  14483.   Easier to use than explain.  It sets the region to an `interesting'
  14484.   block of succeeding lines.  If point is on a blank line, it goes down to
  14485.   the next non-blank line.  That will be the start of the region.  The end
  14486. ***************
  14487. *** 754,766 ****
  14488.    - If a comment, the region will include all succeeding comment lines up
  14489.      to (but not including) the next non-comment line (if any).
  14490.   
  14491. !  - If a code line that opens a new block, the region will include all
  14492. !    succeeding lines up to (but not including) the next code statement
  14493. !    (if any) that's indented no more than the starting line, except that
  14494. !    trailing blank and comment lines are excluded.  E.g., if the starting
  14495. !    line is a `def' statement, the region will be set to the full
  14496. !    function definition, but without any trailing `noise' lines.
  14497.   
  14498.    - Else the region will include all succeeding lines up to (but not
  14499.      including) the next blank line, or code or indenting-comment line
  14500.      indented strictly less than the starting line.  Trailing indenting
  14501. --- 763,789 ----
  14502.    - If a comment, the region will include all succeeding comment lines up
  14503.      to (but not including) the next non-comment line (if any).
  14504.   
  14505. !  - Else if a prefix arg is given, and the line begins one of these
  14506. !    structures:
  14507. ! \tif elif else try except finally for while def class
  14508. !    the region will be set to the body of the structure, including
  14509. !    following blocks that `belong' to it, but excluding trailing blank
  14510. !    and comment lines.  E.g., if on a `try' statement, the `try' block and
  14511. !    all (if any) of the following `except' and `finally' blocks that
  14512. !    belong to the `try' structure will be in the region.  Ditto for
  14513. !    if/elif/else and for/else structures, and (a bit degenerate, since
  14514. !    they're always one-block structures) while, def and class blocks.
  14515.   
  14516. +  - Else if no prefix argument is given, and the line begins a Python
  14517. +    block (see list above), and the block is not a `one-liner' (i.e., the
  14518. +    statement ends with a colon, not with code), the region will include
  14519. +    all succeeding lines up to (but not including) the next code
  14520. +    statement (if any) that's indented no more than the starting line,
  14521. +    except that trailing blank and comment lines are excluded.  E.g., if
  14522. +    the starting line begins a multi-statement `def' structure, the
  14523. +    region will be set to the full function definition, but without any
  14524. +    trailing `noise' lines.
  14525.    - Else the region will include all succeeding lines up to (but not
  14526.      including) the next blank line, or code or indenting-comment line
  14527.      indented strictly less than the starting line.  Trailing indenting
  14528. ***************
  14529. *** 770,779 ****
  14530.   A msg identifying the location of the mark is displayed in the echo
  14531.   area; or do `\\[exchange-point-and-mark]' to flip down to the end.
  14532.   
  14533. ! If called from a program and optional argument JUST-MOVE is not nil,
  14534. ! instead just moves to the end of the block, and does not set mark or
  14535. ! display a msg."
  14536. !   (interactive)
  14537.     (py-goto-initial-line)
  14538.     ;; skip over blank lines
  14539.     (while (and
  14540. --- 793,803 ----
  14541.   A msg identifying the location of the mark is displayed in the echo
  14542.   area; or do `\\[exchange-point-and-mark]' to flip down to the end.
  14543.   
  14544. ! If called from a program, optional argument EXTEND plays the role of the
  14545. ! prefix arg, and if optional argument JUST-MOVE is not nil, just moves to
  14546. ! the end of the block (& does not set mark or display a msg)."
  14547. !   (interactive "P")            ; raw prefix arg
  14548.     (py-goto-initial-line)
  14549.     ;; skip over blank lines
  14550.     (while (and
  14551. ***************
  14552. *** 784,790 ****
  14553.         (error "Hit end of buffer without finding a non-blank stmt"))
  14554.     (let ( (initial-pos (point))
  14555.        (initial-indent (current-indentation))
  14556. !      last-pos)            ; position of last stmt in region
  14557.       (cond
  14558.        ;; if comment line, suck up the following comment lines
  14559.        ((looking-at "[ \t]*#")
  14560. --- 808,821 ----
  14561.         (error "Hit end of buffer without finding a non-blank stmt"))
  14562.     (let ( (initial-pos (point))
  14563.        (initial-indent (current-indentation))
  14564. !      last-pos            ; position of last stmt in region
  14565. !      (followers
  14566. !       '( (if elif else) (elif elif else) (else)
  14567. !          (try except finally) (except except finally) (finally)
  14568. !          (for else)
  14569. !          (def) (class) (while) ) )
  14570. !      first-symbol next-symbol)
  14571.       (cond
  14572.        ;; if comment line, suck up the following comment lines
  14573.        ((looking-at "[ \t]*#")
  14574. ***************
  14575. *** 791,797 ****
  14576.         (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
  14577.         (re-search-backward "^[ \t]*#")    ; and back to last comment in block
  14578.         (setq last-pos (point)))
  14579. !      ;; else if line opens a block, search for next stmt indented <=
  14580.        ((looking-at py-colon-line-re)
  14581.         (while (and
  14582.             (setq last-pos (point))    ; always true -- side effect
  14583. --- 822,844 ----
  14584.         (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
  14585.         (re-search-backward "^[ \t]*#")    ; and back to last comment in block
  14586.         (setq last-pos (point)))
  14587. !      ;; else if line is a block line and EXTEND given, suck up
  14588. !      ;; the whole structure
  14589. !      ((and extend
  14590. !        (setq first-symbol (py-suck-up-first-keyword) )
  14591. !        (assq first-symbol followers))
  14592. !       (while (and
  14593. !           (or (py-goto-beyond-block) t) ; side effect
  14594. !           (forward-line -1)        ; side effect
  14595. !           (setq last-pos (point))    ; side effect
  14596. !           (py-goto-statement-below)
  14597. !           (= (current-indentation) initial-indent)
  14598. !           (setq next-symbol (py-suck-up-first-keyword))
  14599. !           (memq next-symbol (cdr (assq first-symbol followers))))
  14600. !     (setq first-symbol next-symbol)))
  14601. !      ;; else if line *opens* a block, search for next stmt indented <=
  14602.        ((looking-at py-colon-line-re)
  14603.         (while (and
  14604.             (setq last-pos (point))    ; always true -- side effect
  14605. ***************
  14606. *** 798,803 ****
  14607. --- 845,851 ----
  14608.             (py-goto-statement-below)
  14609.             (> (current-indentation) initial-indent))
  14610.       nil))
  14611.        ;; else plain code line; stop at next blank line, or stmt or
  14612.        ;; indenting comment line indented <
  14613.        (t
  14614. ***************
  14615. *** 1165,1170 ****
  14616. --- 1213,1228 ----
  14617.             (not (eobp)))
  14618.       (forward-line 1)))
  14619.   
  14620. + ;; go to point right beyond final line of block begun by the current
  14621. + ;; line.  This is the same as where py-goto-beyond-final-line goes
  14622. + ;; unless we're on colon line, in which case we go to the end of the
  14623. + ;; block.
  14624. + ;; assumes point is at bolp
  14625. + (defun py-goto-beyond-block ()
  14626. +   (if (looking-at py-colon-line-re)
  14627. +       (py-mark-block nil 'just-move)
  14628. +     (py-goto-beyond-final-line)))
  14629.   ;; t iff on continuation line == preceding line ends with backslash
  14630.   ;; that's not in a comment
  14631.   (defun py-continuation-line-p ()
  14632. ***************
  14633. *** 1242,1247 ****
  14634. --- 1300,1313 ----
  14635.        (if (bolp) "" "...")
  14636.        (buffer-substring (point) (progn (end-of-line) (point))))))
  14637.   
  14638. + ;; assuming point at bolp, return first keyword ([a-z]+) on the line,
  14639. + ;; as a Lisp symbol; return nil if none
  14640. + (defun py-suck-up-first-keyword ()
  14641. +   (let ( (case-fold-search nil) )
  14642. +     (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
  14643. +     (intern (buffer-substring (match-beginning 1) (match-end 1)))
  14644. +       nil)))
  14645.   ;; send PROCESS the STRING as input, and wait for the process to send
  14646.   ;; something back
  14647.   (defun py-process-send-string-wait (process string)
  14648. ***************
  14649. *** 1252,1258 ****
  14650.   (defun py-append-to-process-buffer (process string)
  14651.     (save-excursion
  14652.       (let* ( (proc-buf (process-buffer process))
  14653. !         (pop-up-windows t)
  14654.           (proc-win (display-buffer proc-buf)))
  14655.         (set-buffer proc-buf)
  14656.         (goto-char (point-max))
  14657. --- 1318,1324 ----
  14658.   (defun py-append-to-process-buffer (process string)
  14659.     (save-excursion
  14660.       (let* ( (proc-buf (process-buffer process))
  14661. !         (pop-up-windows t)    ; "let*" so display-buffer sees this
  14662.           (proc-win (display-buffer proc-buf)))
  14663.         (set-buffer proc-buf)
  14664.         (goto-char (point-max))
  14665.  
  14666. >>> END OF MSG
  14667. 
  14668. 
  14669. Received: by charon.cwi.nl with SMTP; Mon, 2 Mar 1992 08:55:47 +0100
  14670. Received: from ksr.com ([192.9.200.90]) by hopscotch.ksr.com with SMTP
  14671.     id AA21815; Mon, 2 Mar 1992 02:55:37 -0500
  14672. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  14673.     id AA21967; Mon, 2 Mar 92 02:56:55 EST
  14674. Received: by kaos.ksr.com (4.0/KSR-2.0)
  14675.     id AA12872; Mon, 2 Mar 92 02:56:53 EST
  14676. Message-Id: <9203020756.AA12872@kaos.ksr.com>
  14677. To: python-list@cwi.nl
  14678. Subject: FYI, an exception-handling trick
  14679. Date: Mon, 02 Mar 92 02:56:52 EST
  14680. From: Tim Peters <tim@ksr.com>
  14681.  
  14682. Maybe this is obvious to y'all, and apologies in advance if so.  Wasn't
  14683. obvious to me, and it seems like a pretty slick trick, so ...
  14684.  
  14685. When writing a function that doesn't (or can't) trust its caller to have
  14686. passed good stuff-- when they may have passed something that will cause
  14687. an unpredictable exception, and it's not easy to check that in advance
  14688. --I've been handling it like this:
  14689.  
  14690. try:
  14691.     <dangerous code>
  14692. except:                    # catch every possible goof
  14693.     print 'Unhappy with what you passed!'
  14694.     print 'But it\'s not my fault, it\'s yours!'
  14695.     print 'Wish I could tell you more, but I\'m too lazy to write'
  14696.     print 'an "except" clause for every possible problem.'
  14697.     raise UnhappyError
  14698.  
  14699. What I really want here is some way to pass the original exception up to
  14700. the caller, but to print my own msg first so that the user doesn't
  14701. assume that the problem was caused by my code <grin>.
  14702.  
  14703. This seems to do the trick, & quite pleasantly:
  14704.  
  14705. bad_user = 1                  # assume they screwed up
  14706. try:
  14707.     <dangerous code>
  14708.     bad_user = 0            # guess they didn't -- this time
  14709. finally:
  14710.     if bad_user: print 'Bad user!'
  14711.  
  14712. If nothing goes wrong, bad_user is set to 0 and so the 'finally' block
  14713. just falls through.  If something does go wrong in '<dangerous code>',
  14714. Python aborts executing the 'try' block, so bad_user does not get
  14715. zeroed, so the 'finally' block prints 'Bad user!', and the exception is
  14716. passed up.
  14717.  
  14718. example-attached-ly y'rs  - tim
  14719.  
  14720. Tim Peters   Kendall Square Research Corp
  14721. tim@ksr.com,         ksr!tim@uunet.uu.net
  14722.  
  14723. This is module example.py:
  14724.  
  14725. import regexp
  14726.  
  14727. lambda_recognizer = \
  14728.       regexp.compile( '^[ \t]*lambda[ \t]*\(' ).match
  14729.  
  14730. def map( l, f ):
  14731.     if type(f) is type(''):
  14732.  
  14733.     try: paren_index = lambda_recognizer(f)[0][1] - 1
  14734.     except:
  14735.         print 'Error in map, arg 2:  string not a lambda'
  14736.         raise ValueError, `f`
  14737.  
  14738.     f = 'def f' + f[paren_index:]    # 'lambda' -> 'def f'
  14739.     bad = 1                # guilty until proven innocent
  14740.     try:
  14741.         exec( f + '\n' )        # creates local func 'f'
  14742.         bad = 0
  14743.     finally:
  14744.         if bad:
  14745.         print 'Error in map, arg 2:  ' + \
  14746.               'couldn\'t exec ' + `f`
  14747.  
  14748.     ans = []
  14749.     for elt in l: ans.append( f(elt) )
  14750.     return ans
  14751.  
  14752. And a short session illustrating it:
  14753.  
  14754. >>> from example import map
  14755. >>> a = [1, 2, 8]
  14756. >>> def p1(n): return n+1
  14757. ...
  14758. >>> map( a, p1 )
  14759. [2, 3, 9]
  14760. >>> map( a, 'lambda(n): return n*n' )
  14761. [1, 4, 64]
  14762. >>> map( a, 'lamda(n): return n*n' )
  14763. Error in map, arg 2:  string not a lambda
  14764. Unhandled exception: ValueError: 'lamda(n): return n*n'
  14765. Stack backtrace (innermost last):
  14766.   File "<stdin>", line 1
  14767.   File "./example.py", line 12
  14768.     raise ValueError, `f`
  14769. >>> map( a, 'lambda(n): return n*' )
  14770. Error in map, arg 2:  couldn't exec 'def f(n): return n*'
  14771. Unhandled exception: SyntaxError: invalid syntax
  14772. Stack backtrace (innermost last):
  14773.   File "<stdin>", line 1
  14774.   File "./example.py", line 17
  14775.     exec( f + '\n' )        # creates local func 'f'
  14776. >>> END OF MSG
  14777. 
  14778. 
  14779. Received: by charon.cwi.nl with SMTP; Mon, 2 Mar 1992 20:48:02 +0100
  14780. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa13587;
  14781.           2 Mar 92 14:47 EST
  14782. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  14783.     id AA11623; Mon, 2 Mar 92 14:47:08 EST
  14784. Date: Mon, 2 Mar 92 14:47:08 EST
  14785. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  14786. Message-Id: <9203021947.AA11623@aemsun.med.Virginia.EDU>
  14787. To: tim@ksr.com
  14788. Subject: Re:  FYI, an exception-handling trick
  14789. Cc: python-list@cwi.nl
  14790.  
  14791. tim@ksr.com>
  14792. > Maybe this is obvious to y'all, and apologies in advance if so.  Wasn't
  14793. > obvious to me, and it seems like a pretty slick trick, so ...
  14794. >
  14795. > When writing a function that doesn't (or can't) trust its caller to have
  14796. > passed good stuff-- when they may have passed something that will cause
  14797. > an unpredictable exception, and it's not easy to check that in advance
  14798. > --I've been handling it like this:
  14799. > try:
  14800. >     <dangerous code>
  14801. > except:                    # catch every possible goof
  14802. >     print 'Unhappy with what you passed!'
  14803. >     print 'But it\'s not my fault, it\'s yours!'
  14804. >     print 'Wish I could tell you more, but I\'m too lazy to write'
  14805. >     print 'an "except" clause for every possible problem.'
  14806. >     raise UnhappyError
  14807. > What I really want here is some way to pass the original exception up to
  14808. > the caller, but to print my own msg first so that the user doesn't
  14809. > assume that the problem was caused by my code <grin>.
  14810.  
  14811. Well, there USED to be an easy ( but not-documented, so not obvious )
  14812. way - the sys module contained the objects sys.exc_type & sys.exc_value,
  14813. so I used to be able to: 
  14814.  
  14815. try:
  14816.    <dangerous code>
  14817. except: 
  14818.    print my_msg
  14819.    raise sys.exc_type,sys.exc_value 
  14820.  
  14821.  
  14822. This WAS an undocumented feature, but it seems to have disappeared ( or 
  14823. moved ? ). "import sys; dir(sys)" no longer shows exc_type & exc_value. 
  14824.  
  14825. I was at first quite distressed to discover that this feature was now
  14826. missing.  I know I'm looking for trouble relying on an undocumented
  14827. feature ... I don't mean I'm distressed because I have to change my
  14828. code. If it were moved and renamed, I wouldn't complain. [ As is is, 
  14829. I'm trying to keep my complaint muted, because although I like the 
  14830. functionality of the feature, I must admit it is sort of a kludge. It
  14831. might be better to have the exception be locally assignable as the 
  14832. optional argument is, not a "global" symbol in module sys. ] 
  14833. But now, it is quite awkward to write exception handlers that have 
  14834. common code for several exceptions. (minor). AND I don't see any way 
  14835. to write a generic exception handler that re-raises some of all of its
  14836. exceptions to an outer handler. (major). One has to make an exhaustive
  14837. list of exceptions, and any new ones not on the list will not be caught.
  14838.  
  14839. I see there are some notes about changes to error handling in error.c.
  14840. I assume that this change ( disappearance of sys.exc_type ) is related
  14841. to the changes in that code. (?)
  14842.  
  14843. My first impression is that I don't like this. 
  14844. [ I admit I'm biased by growing up on VAX/VMS error handling, which 
  14845. are a bit more "full featured" than vanilla C/UNIX signals. ]
  14846.  
  14847. - Steve 
  14848.  
  14849.  
  14850. [ Sorry Tim, but your kludge around it looks even uglier! :-(  ] 
  14851. 
  14852. 
  14853. Received: by charon.cwi.nl with SMTP; Tue, 3 Mar 1992 09:57:12 +0100
  14854. Received: from ksr.com ([192.9.200.90]) by hopscotch.ksr.com with SMTP
  14855.     id AA25884; Tue, 3 Mar 1992 03:57:13 -0500
  14856. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  14857.     id AA25944; Tue, 3 Mar 92 03:58:35 EST
  14858. Received: by kaos.ksr.com (4.0/KSR-2.0)
  14859.     id AA23041; Tue, 3 Mar 92 03:58:34 EST
  14860. Message-Id: <9203030858.AA23041@kaos.ksr.com>
  14861. To: sdm7g@Virginia.EDU, guido@cwi.nl
  14862. Subject: Re:  FYI, an exception-handling trick
  14863. Cc: python-list@cwi.nl
  14864. Date: Tue, 03 Mar 92 03:58:33 EST
  14865. From: Tim Peters <tim@ksr.com>
  14866.  
  14867. > ... there USED to be an easy ( but not-documented, so not obvious )
  14868. > way - the sys module contained the objects sys.exc_type & sys.exc_value
  14869.  
  14870. Just noting that several of the library modules (e.g., lib/importall.py)
  14871. still reference these guys.  I don't see them in sys either, although
  14872. they still show up in "strings" output ... hmm ... check this out!:
  14873.  
  14874. Python 0.9.4 alpha (>= Dec 24 1991).
  14875. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  14876. >>> import sys
  14877. >>> dir(sys)  # not there
  14878. ['argv', 'exit', 'modules', 'path', 'ps1', 'ps2', 'stderr', 'stdin', 'stdout', 'version']
  14879. >>> oldsys = dir(sys)
  14880. >>> def newsys():    # return any new things that show up in sys.*
  14881. ...     ans = []
  14882. ...     for elt in dir(sys):
  14883. ...         if elt not in oldsys: ans.append(elt)
  14884. ...     return ans
  14885. ...
  14886. >>> 1/0
  14887. Unhandled exception: ZeroDivisionError: integer division
  14888. Stack backtrace (innermost last):
  14889.   File "<stdin>", line 1
  14890. >>> newsys()   # hmm!  more secret stuff for Steve <grin>
  14891. ['last_traceback']
  14892. >>> def inner(): return 1/0
  14893. ...
  14894. >>> def outer():
  14895. ...     try: return inner()
  14896. ...     except TypeError: pass
  14897. ...
  14898. >>> outer()
  14899. Unhandled exception: ZeroDivisionError: integer division
  14900. Stack backtrace (innermost last):
  14901.   File "<stdin>", line 1
  14902.   File "<stdin>", line 2
  14903.   File "<stdin>", line 1
  14904. >>> newsys()    # voila!
  14905. ['exc_traceback', 'exc_type', 'exc_value', 'last_traceback']
  14906. >>> sys.exc_type
  14907. 'ZeroDivisionError'
  14908. >>> sys.exc_value
  14909. 'integer division'
  14910. >>>
  14911.  
  14912. Haven't been able to get 'em to show up with anything simpler than that
  14913. (i.e., I seem to need to have at least one group of 'except' blocks
  14914. *not* catch the exception, and unwind at least one call level).
  14915.  
  14916. So they're there, but hard to guess when <grin>.  Yo, Guido, feature or
  14917. bug?  I didn't know about them before, so can't tell whether this is the
  14918. way they always worked.  Offhand it sure looks like it would be more
  14919. pleasant/useful if they were set every time there's an exception ...
  14920.  
  14921. >  ... One has to make an exhaustive list of exceptions, and any new
  14922. >  ones not on the list will not be caught.
  14923.  
  14924. No argument from me, Steve -- seems to me that exception-handling gets
  14925. used when I have a very specific potential problem (in which case one or
  14926. two specific 'except' blocks do the trick), or when I have no idea what
  14927. might go wrong but don't want to crash regardless (in which case an
  14928. exhaustive list is just impossible to maintain as things change).
  14929.  
  14930. >  ...
  14931. >  [ Sorry Tim, but your kludge around it looks even uglier! :-(  ]
  14932.  
  14933. as-a-long-time-fortran-programmer-i'm-immune-to-compliments<grin>-ly
  14934.    y'rs  - tim
  14935.  
  14936. Tim Peters   Kendall Square Research Corp
  14937. tim@ksr.com,         ksr!tim@uunet.uu.net
  14938. 
  14939. 
  14940. To: Tim Peters <tim@ksr.com>
  14941. Subject: Re: Deadly problem in src/strtod.c 
  14942. In-reply-to: Your message of "Tue, 03 Mar 1992 03:12:49 MET."
  14943.              <9203030812.AA22718@kaos.ksr.com> 
  14944. From: Guido van Rossum <guido@cwi.nl>
  14945. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  14946. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  14947. Date: Tue, 03 Mar 1992 10:16:54 +0100
  14948. Sender: guido
  14949.  
  14950. Yeah, my reliance on strtol/strtod has kept causing problems.  I've
  14951. finally decided to provide complete, working PD implementations of
  14952. each in the next release.  Source attached, in case you'd like to have
  14953. a look at their robustness.
  14954.  
  14955. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  14956. "Kemal Ataturk had an entire menagerie called Abdul."
  14957. 
  14958. 
  14959. Received: by charon.cwi.nl with SMTP; Tue, 3 Mar 1992 13:07:43 +0100
  14960. Received: by voorn.cwi.nl with SMTP; Tue, 3 Mar 1992 12:07:40 GMT
  14961. Message-Id: <9203031207.AA26336@voorn.cwi.nl>
  14962. To: python-list@cwi.nl
  14963. Subject: sys.exc_type etc.
  14964. In-Reply-To: Your message of "Tue, 03 Mar 1992 03:58:33 MET."
  14965.              <9203030858.AA23041@kaos.ksr.com> 
  14966. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  14967. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  14968. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  14969. Date: Tue, 03 Mar 1992 13:07:37 +0100
  14970. Sender: Guido.van.Rossum@cwi.nl
  14971.  
  14972. Looks like Steve and Tim have uncovered another murky area in Python...
  14973.  
  14974. To summarize the story so far for our new readers, and for those who
  14975. may not yet have acquired the Python fluency needed to read long
  14976. listings of Python code exemplifying a particular point:
  14977.  
  14978. (1) Tim wants to catch an exception, print some message, and then
  14979. re-raise the same exception, without ever caring *which* exception he
  14980. caught, as long as he re-raises the same one that he caught.  He
  14981. discovers that this is possible using try...finally and a Boolean
  14982. variable.
  14983.  
  14984. (2) Steven replies that there used to be undocumented variables
  14985. sys.exc_type and sys.exc_value identifying the most recent exception,
  14986. which could be used to re-raise the exception, but he can't find them
  14987. in his current Python version.  He also remarks that this is cleaner
  14988. than using try...finally.
  14989.  
  14990. (3) Tim discovers that sys.exc_type etc. still exist, but only get set
  14991. under obscure circumstances involving at least one try...except block
  14992. that does not catch the exception, and one stack unwind.  He then
  14993. expresses the wish that sys.exc_type etc. be set each time an
  14994. exception occurs.
  14995.  
  14996. Now my comments:
  14997.  
  14998. First, I think that Tim's original solution to his problem is
  14999. preferable over Steven's version, because Tim's preserves the
  15000. traceback: in Steven's code the traceback printed by the interpreter
  15001. ends at the "raise sys.exc_type, sys.exc_value" statement in his
  15002. except clause, which isn't very helpful for finding the cause of the
  15003. problem.  Using "finally", on the other hand, preserves the original
  15004. traceback (which is, incidentally, saved as sys.exc_traceback in some
  15005. versions of the interpreter).
  15006.  
  15007. Next, an explanation of how sys.exc_type etc. work is in order.  I
  15008. added them as an afterthought when I found out that if an exception
  15009. handler handles several types of exceptions at a time, it often needs
  15010. to know which exception occurred (if only to print the right message).
  15011. I played with the thought of changing the syntax so the handler could
  15012. specify a variable in which to receive the exception type (just like
  15013. it can already do for the exception's "parameter" or "associated
  15014. value"), but quickly realized that the feature is most needed for
  15015. default "except:" clauses, where adding new syntax was impossible to
  15016. do to in an elegant, yet backward-compatible way!  At that time,
  15017. changing the syntax in an incompatible way would already be rather
  15018. painful, so I opted for some secret variables.  (And forgot to
  15019. document them properly -- sorry about that!)
  15020.  
  15021. Now, the choice of *when* sys.exc_type is set must be explained.  (I
  15022. won't mention sys.exc_value again, since it is set whenever
  15023. sys.exc_type is set.)  Tim thinks he wants them set each time an
  15024. exception occurs, but there's a problem with that.  When searching for
  15025. an exception handler, the interpreter may execute a lot of user code:
  15026. evaluating the expressions in except clause headers, but also finally
  15027. clauses of inner try statements.  All this code may itself raise
  15028. exceptions, as long as it catches them -- so if sys.exc_type were set
  15029. each time an exception occured, an outer exception handle might see
  15030. the wrong exception type.  For example:
  15031.  
  15032.     try:
  15033.         try:
  15034.             1/0
  15035.         finally:
  15036.             try:
  15037.                 raise EOFError
  15038.             except:
  15039.                 pass
  15040.     except:
  15041.         print sys.exc_type, ':', sys.exc_value
  15042.  
  15043. The solution is to set sys.exc_type at the moment the exception
  15044. handler is entered.  (Note that internally in the interpreter, there
  15045. is no confusion between exceptions occurring in nested pieces of code
  15046. -- it keeps them on its internal stack.)  Because of the way the
  15047. interpreted code is generated, the interpreter actually assigns to
  15048. sys.exc_type each time it finds there are except clauses to consider,
  15049. just before evaluating the first clause's exception or list of
  15050. exceptions.  This is not done for finally clauses -- these are treated
  15051. completely different from except clauses.  I know I have changed the
  15052. policy that determines when sys.exc_type is set several times; in my
  15053. own, still experimental version (which I call 0.9.6) it is set
  15054. independent of whether the exception is being handled or not.  The
  15055. simplest case that sets it is:
  15056.  
  15057.     try:
  15058.         1/0
  15059.     except:
  15060.         pass
  15061.  
  15062. It also works with "except RuntimeError:" or "except EOFError:" instead
  15063. of "except:".  I mayu have changed this since 0.9.4 (the latest
  15064. distributed version) in order to support the new tracer/debugger
  15065. feature.
  15066.  
  15067. It is debatable whether sys.exc_type should be set when there is no
  15068. except clause and the user gets thrown back in the interactive main
  15069. loop.  This isn't done currently, because I haven't encountered a need
  15070. for it yet, but I could easily added if there is justified demand.  It
  15071. might be better to use different variable names in this case: I
  15072. already save the traceback info, under the name sys.last_traceback.
  15073. The distinction is useful: like sys.exc_type, sys.exc_traceback
  15074. contains rather volatile information (overwritten each time an
  15075. exception gets handled), while sys.last_traceback remains valid until
  15076. the next primary prompt, at least.
  15077.  
  15078. In fact, this made so much sense that I've gone to the source and
  15079. added sys.last_type and sys.last_value.  I've also documented all six
  15080. variables in the Library Reference.  So there's no need to change
  15081. existing, working code that uses any of these, unless this message
  15082. triggers a discussion that results in a cleaner design or better names
  15083. or whatever other changes might be needed -- folks, speak now or be
  15084. silent forever! :-)
  15085.  
  15086. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  15087. "One's never alone with a rubber duck."
  15088. 
  15089. 
  15090. Received: by charon.cwi.nl with SMTP; Tue, 3 Mar 1992 21:18:08 +0100
  15091. Received: from prg.oxford.ac.uk by eros.uknet.ac.uk via JANET with NIFTP (PP) 
  15092.           id <5202-0@eros.uknet.ac.uk>; Tue, 3 Mar 1992 19:43:04 +0000
  15093. Received: from lucrece.robots (lucrece-gate.robots) by prg.oxford.ac.uk 
  15094.           id AA03690; Tue, 3 Mar 92 19:42:23 GMT
  15095. Received: by uk.ac.oxford.robots (4.1/Robots 1.2m (20-Jun-90)) id AA07141;
  15096.           Tue, 3 Mar 92 19:40:31 GMT
  15097. Date: Tue, 3 Mar 92 19:40:31 GMT
  15098. From: peter@robots.oxford.ac.uk (Peter Ho)
  15099. Message-Id: <9203031940.AA07141@uk.ac.oxford.robots>
  15100. To: python-list@cwi.nl
  15101. Subject: Re: sys.exc_type etc.
  15102.  
  15103. I was wondering whether the problem of the nested exceptions could be
  15104. handled the way you let the interpreter handles it.  Guido says,
  15105.     (Note that internally in the interpreter, there is no confusion
  15106.     between exceptions occurring in nested pieces of code -- it keeps them
  15107.     on its internal stack.)
  15108. As you pop this internal stack would it be possible to restore the exeception
  15109. type in sys.exc_type?
  15110.  
  15111.         Just a thought....,
  15112.             Pete. :-)
  15113. 
  15114. 
  15115. Received: by charon.cwi.nl with SMTP; Wed, 4 Mar 1992 06:28:20 +0100
  15116. Received: from ksr.com ([192.9.200.90]) by hopscotch.ksr.com with SMTP
  15117.     id AA29283; Wed, 4 Mar 1992 00:28:18 -0500
  15118. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  15119.     id AA29222; Wed, 4 Mar 92 00:29:41 EST
  15120. Received: by kaos.ksr.com (4.0/KSR-2.0)
  15121.     id AA08354; Wed, 4 Mar 92 00:29:40 EST
  15122. Message-Id: <9203040529.AA08354@kaos.ksr.com>
  15123. To: guido@cwi.nl
  15124. Subject: Re: sys.exc_type etc.
  15125. Cc: python-list@cwi.nl
  15126. Date: Wed, 04 Mar 92 00:29:39 EST
  15127. From: Tim Peters <tim@ksr.com>
  15128.  
  15129. >  ...
  15130. >  I played with the thought of changing the syntax so the handler could
  15131. >  specify a variable in which to receive the exception type (just like
  15132. >  it can already do for the exception's "parameter" or "associated
  15133. >  value"), but quickly realized that the feature is most needed for
  15134. >  default "except:" clauses, where adding new syntax was impossible to
  15135. >  do to in an elegant, yet backward-compatible way!
  15136.  
  15137. Two things here:
  15138.  
  15139. The current syntax is a bit error-prone already.  E.g., if I run this:
  15140.  
  15141. try:
  15142.     print kidding
  15143. except NameError:
  15144.     print 'No kidding!'
  15145.  
  15146. it (unsurprisingly) prints 'No kidding!'.  But if I put another chunk of
  15147. code before it like so:
  15148.  
  15149. try:
  15150.     1/0
  15151. except ZeroDivisionError, NameError:
  15152.     print 'caught the division'
  15153.  
  15154. try:
  15155.     print kidding
  15156. except NameError:
  15157.     print 'No kidding!'
  15158.  
  15159. I get:
  15160.  
  15161. caught the division
  15162. Unhandled exception: NameError: kidding
  15163. Stack backtrace (innermost last):
  15164.   File "exc1.py", line 7
  15165.     print kidding
  15166.  
  15167. The difference between the incorrect (catches only divide-by-zero, & if
  15168. that happens binds the local name 'NameError' to the argument of the
  15169. ZeroDivisionError exception):
  15170.  
  15171.     except ZeroDivisionError, NameError:
  15172.  
  15173. above and the correct (does what most people thought the one above would
  15174. do <grin>):
  15175.  
  15176.     except (ZeroDivisionError, NameError):
  15177.  
  15178. is a bit subtle, and hard to "see" when you make the mistake.
  15179.  
  15180. Second, if you were to change the syntax to allow specifying 'type' and
  15181. 'value' variables, I think the place to do it is not on the 'except'
  15182. clauses but on the 'try' statement itself.  I.e.,
  15183.  
  15184.     try optional_name_or_two-tuple:
  15185.  
  15186. where optional_name_or_two-tuple is bound to an appropriate (type,value)
  15187. tuple if an exception occurred, and, for the benefit of 'finally'
  15188. blocks (& maybe too for following code ...), is bound to (None,None) if
  15189. no exception occurred.  E.g.,
  15190.  
  15191.     try exctype, excval:
  15192.         blah blah blah
  15193.     except NameError:
  15194.         # exctype is 'NameError' here, excval is the detail
  15195.     except (TypeError, EOFError), msg:
  15196.         # exctype is whichever of the two occurred, &
  15197.         # for compatibility w/ the current scheme both msg
  15198.         # & excval hold the detail
  15199.     except:
  15200.         # exctype has the type, excval the detail
  15201.     finally:    # note that the boolean-vrbl trick isn't needed
  15202.         if exctype:
  15203.             print 'bad user!  you screwed up in a way'
  15204.             print 'i couldn\'t predict!'
  15205.  
  15206. Having exctype available in the 'finally' block might also be handy if
  15207. clean-up actions depend on the nature of the exception.
  15208.  
  15209. >  ... Tim thinks he wants them [sys.exc_type etc] set each time an
  15210. >  exception occurs, but there's a problem with that.
  15211.  
  15212. "Each time", yes, but I didn't say exactly *when* "each time" <grin>.
  15213. Agree that setting them at the instant the exception occurs would lead
  15214. to problems.
  15215.  
  15216. >  The solution is to set sys.exc_type at the moment the exception
  15217. >  handler is entered.
  15218.  
  15219. Not sure that's quite it, either, but am sure it's half of it.
  15220.  
  15221. try:
  15222.     print kidding
  15223. except NameError:
  15224.     try: 1/0
  15225.     except:
  15226.     print 'sys.exc_type should certainly be a zero-divide here'
  15227.     # but what should sys.exc_type be here?
  15228.  
  15229. I believe what you sketched would leave me thinking I was staring at a
  15230. divide-by-zero error at the position of the comment.  But I still want
  15231. to see a NameError at that point -- the divide by zero is history at
  15232. that point.  In nested structures, I always want to see the stuff that's
  15233. relevant to the smallest *enclosing* structure; & at the position of the
  15234. comment, that's the 'try' block that suffered the name error.  The point
  15235. to that may be clearer by replacing the block at the end; I don't want
  15236. to have to know what somefunc does:
  15237.  
  15238. except NameError:
  15239.     somefunc()
  15240.     # what should sys.exc_type be here?
  15241.  
  15242. I believe Peter Ho's comment leads to the same end here, hence there's
  15243. universal convergence <grin>.  One funny side-effect:  I believe this
  15244. *implies* that sys.exc_type and sys.exc_value would always be None (or
  15245. unbound, or something else equally vacuous) if printed from a top-level
  15246. prompt (the interpreter stack is completely unwound; there is no
  15247. exception pending in the "smallest enclosing structure" because there is
  15248. no enclosing structure).  But maybe that's just another way of saying
  15249. that both the sys.exc_... and sys.last_... flavors are useful.
  15250.  
  15251. >  ... folks, speak now or be silent forever! :-)
  15252.  
  15253. Dream on <snort>.
  15254.  
  15255.  
  15256. A related question, but on a different tack.  The following program
  15257. (correctly) dies with an IndexError on the "except elist[i], val:" line:
  15258.  
  15259. elist = (None, NameError)
  15260.  
  15261. def absurd(i):
  15262.     try:
  15263.     try:
  15264.         1/0
  15265.     except DivideByZeroError:    # meant ZeroDivisionError
  15266.         print 'caught an exception that doesn\'t exist!'
  15267.     except elist[i], val:
  15268.     print 'caught the', elist[i], 'error on', val
  15269.  
  15270. absurd(2)
  15271.  
  15272. The details of that silly code are irrelevant.  The *point* is that at
  15273. the time it dies, it's trying to handle an unhandled NameError exception
  15274. from the "except DivideByZeroError" line, while that in turn was trying
  15275. to handle the unhandled ZeroDivisionError.  It bugs me (a little, & more
  15276. in the abstract sense) that the unresolved ZeroDivisionError and
  15277. NameError exceptions go unreported.  Then again, I'd never write code
  15278. with an error in an exception-handler <grin> ...
  15279.  
  15280. verbally y'rs  - tim
  15281.  
  15282. Tim Peters   Kendall Square Research Corp
  15283. tim@ksr.com,         ksr!tim@uunet.uu.net
  15284. 
  15285. 
  15286. Received: by charon.cwi.nl with SMTP; Thu, 5 Mar 1992 17:20:17 +0100
  15287. Received: from prg.oxford.ac.uk by eros.uknet.ac.uk via JANET with NIFTP (PP) 
  15288.           id <3837-0@eros.uknet.ac.uk>; Thu, 5 Mar 1992 14:12:48 +0000
  15289. Received: from lucrece.robots (lucrece-gate.robots) by prg.oxford.ac.uk 
  15290.           id AA26750; Thu, 5 Mar 92 14:12:00 GMT
  15291. Received: from robots.ox.ac.uk (miranda.robots) 
  15292.           by uk.ac.oxford.robots (4.1/Robots 1.2m (20-Jun-90)) id AA08313;
  15293.           Thu, 5 Mar 92 14:10:10 GMT
  15294. Received: by robots.ox.ac.uk (4.1/robots.remoteV2.0) id AA03434;
  15295.           Thu, 5 Mar 92 14:10:29 GMT
  15296. Date: Thu, 5 Mar 92 14:10:29 GMT
  15297. From: peter@robots.oxford.ac.uk
  15298. Message-Id: <9203051410.AA03434@miranda.robots.ox.ac.uk>
  15299. To: python-list@cwi.nl
  15300. Subject: Re: sys.exc_type etc.
  15301.  
  15302. |     try optional_name_or_two-tuple:
  15303. |
  15304. | Having exctype available in the 'finally' block might also be handy if
  15305. | clean-up actions depend on the nature of the exception.
  15306.  
  15307. I like this idea of having optional exc_{type,val} variables.
  15308.  
  15309. | try:
  15310. |     print kidding
  15311. | except NameError:
  15312. |     try: 1/0
  15313. |     except:
  15314. |     print 'sys.exc_type should certainly be a zero-divide here'
  15315. |     # but what should sys.exc_type be here?
  15316. | I believe what you sketched would leave me thinking I was staring at a
  15317. | divide-by-zero error at the position of the comment.  But I still want
  15318. | to see a NameError at that point -- the divide by zero is history at
  15319. | that point.  In nested structures, I always want to see the stuff that's
  15320. | relevant to the smallest *enclosing* structure; & at the position of the
  15321. | comment, that's the 'try' block that suffered the name error.
  15322. |
  15323. | A related question, but on a different tack.  The following program
  15324. | (correctly) dies with an IndexError on the "except elist[i], val:" line:
  15325. | elist = (None, NameError)
  15326. | def absurd(i):
  15327. |     try:
  15328. |     try:
  15329. |         1/0
  15330. |     except DivideByZeroError:    # meant ZeroDivisionError
  15331. |         print 'caught an exception that doesn\'t exist!'
  15332. |     except elist[i], val:
  15333. |     print 'caught the', elist[i], 'error on', val
  15334. | absurd(2)
  15335. | The details of that silly code are irrelevant.  The *point* is that at
  15336. | the time it dies, it's trying to handle an unhandled NameError exception
  15337. | from the "except DivideByZeroError" line, while that in turn was trying
  15338. | to handle the unhandled ZeroDivisionError.
  15339.  
  15340. These two examples suggest to me that it is not easy to define exactly
  15341. what the smallest enclosing structure is.  Why do you consider the current
  15342. behaviour (raising IndexError) correct, whereas in the first example you say
  15343. NameError should be the correct exception returned.
  15344.     I do see your points, but having exceptions set according to where
  15345. the code is (e.g in an exception block or a try block) may make the code
  15346. difficult to understand, and the compiler could approach the complexity of
  15347. Perl ;-)
  15348.  
  15349. | I believe Peter Ho's comment leads to the same end here, hence there's
  15350. | universal convergence <grin>.  One funny side-effect:  I believe this
  15351. | *implies* that sys.exc_type and sys.exc_value would always be None (or
  15352. | unbound, or something else equally vacuous)
  15353.  
  15354. Whoops :-)
  15355.  
  15356. | >  ... folks, speak now or be silent forever! :-)
  15357.  
  15358. That's it folks,
  15359.     Pete.
  15360. 
  15361. 
  15362. Received: by charon.cwi.nl with SMTP; Fri, 6 Mar 1992 03:29:16 +0100
  15363. Received: from ksr.com ([192.9.200.90]) by hopscotch.ksr.com with SMTP
  15364.     id AA05453; Thu, 5 Mar 1992 21:26:37 -0500
  15365. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  15366.     id AA17548; Thu, 5 Mar 92 21:28:04 EST
  15367. Received: by kaos.ksr.com (4.0/KSR-2.0)
  15368.     id AA05565; Thu, 5 Mar 92 21:28:02 EST
  15369. Message-Id: <9203060228.AA05565@kaos.ksr.com>
  15370. To: peter@robots.ox.ac.uk
  15371. Subject: Re: sys.exc_type etc.
  15372. Cc: python-list@cwi.nl
  15373. Date: Thu, 05 Mar 92 21:28:01 EST
  15374. From: Tim Peters <tim@ksr.com>
  15375.  
  15376. >  | [tim suggests the following as a way to introduce optional
  15377. >  |  exception type-&-value-holding names w/o busting current code]
  15378. >  |     try optional_name_or_two-tuple:
  15379. >  | ...
  15380. >  I like this idea of having optional exc_{type,val} variables.
  15381.  
  15382. Me too, Pete, but I don't really like this syntax.  Guido hasn't said
  15383. anything but I suspect he thinks its inelegant; I'd agree (sticking the
  15384. names on the 'try' interferes with reading the non-exceptional path).
  15385.  
  15386. >  | try:
  15387. >  |     print kidding
  15388. >  | except NameError:
  15389. >  |     try: 1/0
  15390. >  |     except:
  15391. >  |         print 'sys.exc_type should certainly be a zero-divide here'
  15392. >  |     # but what should sys.exc_type be here?
  15393. >  |     [tim sez NameError]
  15394. >  | ...
  15395. >  |
  15396. >  | elist = (None, NameError)
  15397. >  |
  15398. >  | def absurd(i):
  15399. >  |     try:
  15400. >  |          try:
  15401. >  |             1/0
  15402. >  |          except DivideByZeroError:    # meant ZeroDivisionError
  15403. >  |              print 'caught an exception that doesn\'t exist!'
  15404. >  |     except elist[i], val:
  15405. >  |          print 'caught the', elist[i], 'error on', val
  15406. >  |
  15407. >  | absurd(2)
  15408. >  | [dies with an index error, but a name error and a divide-by-zero
  15409. >  |  error are also unhandled at this point]
  15410.  
  15411. >  These two examples suggest to me that it is not easy to define exactly
  15412. >  what the smallest enclosing structure is.
  15413.  
  15414. I don't think the examples were related:  the first was talking about
  15415. what to do when the user handles an exception, the second about what to
  15416. do when the user fails to handle the exception.  So they're very
  15417. different at the outset.
  15418.  
  15419. "Smallest enclosing structure" was an unfortunate phrasing in any case;
  15420. it happened to be appropriate for the specific example, but the intended
  15421. behavior (which I believe is the same as yours) is really more dynamic
  15422. than lexical.  It's hard to be precise here unless/until there's a more-
  15423. or-less formal execution model for Python; the intended behavior would
  15424. (I think) be easy to define as a modification to that.
  15425.  
  15426. Informally, I would like the value of sys.exc_* at a given program point
  15427. P to be defined by the following (which, in the absence of an execution
  15428. model, is phrased in a clumsy "bottom up" way):
  15429.  
  15430. 1.  Is P in an 'except' or 'finally' block?
  15431.     If yes, then "smallest enclosing" is well-defined (in the obvious
  15432.     (lexical) way!), and I want sys.exc_* to reflect the exception (if
  15433.     any) that aborted the associated 'try' block.
  15434.  
  15435.     Else go to step 2.
  15436.  
  15437.     Subtlety:  the "(if any)" was for the benefit of 'finally' blocks,
  15438.     which may be entered even if their associated 'try' block did not
  15439.     abort.  In that case I expect sys.exc_* to be None.
  15440.  
  15441. 2.  [the dynamic component]
  15442.     Was I called by anyone?
  15443.     If 'yes', go back to step 1 after setting P to the call site.
  15444.     Else (at top level) sys.exc_* should be None.
  15445.  
  15446. If we had a detailed account of how Python manages its internal
  15447. exception stack, I suspect this might all (except for the treatment of
  15448. 'finally' blocks) boil down to that 'sys.exc_*' should mirror whatever
  15449. happens to be at the top of the exception stack from moment to moment.
  15450.  
  15451. Another way to say it:  Suppose we had the optional
  15452.  
  15453.     try exc_type, exc_val:
  15454.  
  15455. syntax.  Ignoring the bits about crawling up the call stack, I would
  15456. like sys.exc_* to return exactly what exc_type/exc_val would return at
  15457. every point, provided that I used a uniquely-named exc_type & exc_val
  15458. for each distinct 'try' block.  It's just a way to get at part of the
  15459. same functionality without introducing any new syntax.
  15460.  
  15461. >  Why do you consider the current behaviour (raising IndexError)
  15462. >  correct, whereas in the first example you say NameError should be the
  15463. >  correct exception returned.
  15464.  
  15465. Because they're the right things to do <grin>.  Here I think you're
  15466. comparing what sys.exc_* should be *bound to* when the user is
  15467. explicitly handling an exception (1st example) to what Python itself
  15468. should *do* when the user fails to handle the exception (2nd example) --
  15469. I just don't see them as being related.
  15470.  
  15471. Note that I would also consider other behaviors to be "correct" in the
  15472. 2nd example.  When faced with multiple unhandled exceptions, Python
  15473. currently appears to report only the most recent (in time); reporting
  15474. all or reporting some or reporting only the least recent ... etc are all
  15475. OK by me, just so long as *something* gets reported.  I suspect the
  15476. current behavior is more an accident of implementation than the result
  15477. of deliberate design, though, so thought it might be interesting to
  15478. consider other behaviors.
  15479.  
  15480. >      I do see your points, but having exceptions set according to where
  15481. >  the code is (e.g in an exception block or a try block) may make the code
  15482. >  difficult to understand,
  15483.  
  15484. Sorry, I need examples here; couldn't follow your thought.
  15485.  
  15486. > and the compiler could approach the complexity of Perl ;-)
  15487.  
  15488. God forbid!  My hope/belief is that I'm trying to find a way to get at
  15489. something Python already knows anyway (and indeed *must* know else it
  15490. couldn't itself keep the exceptions straight ...).
  15491.  
  15492. >  | ... I believe this *implies* that sys.exc_type and sys.exc_value
  15493. >  | would always be None (or unbound, or something else equally
  15494. >  | vacuous) [when printed from a top-level prompt]
  15495. >
  15496. >  Whoops :-)
  15497.  
  15498. Na, Guido said that's already the way Python acts -- we're just trying
  15499. to give him an elaborate theoretical justification for keeping it that
  15500. way <grin>.
  15501.  
  15502. skating-on-exceptional-ice-ly y'rs  - tim
  15503.  
  15504. Tim Peters   Kendall Square Research Corp
  15505. tim@ksr.com,         ksr!tim@uunet.uu.net
  15506. 
  15507. 
  15508. Received: by charon.cwi.nl with SMTP; Fri, 6 Mar 1992 16:19:33 +0100
  15509. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa03465;
  15510.           6 Mar 92 10:14 EST
  15511. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  15512.     id AA16640; Fri, 6 Mar 92 10:13:57 EST
  15513. Date: Fri, 6 Mar 92 10:13:57 EST
  15514. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  15515. Message-Id: <9203061513.AA16640@aemsun.med.Virginia.EDU>
  15516. To: python-list@cwi.nl
  15517. Subject: Re: sys.exc_type etc.
  15518.  
  15519. I my last post, I started to add some comments on how Python exceptions
  15520. *might* be "fixed"... ,but I edited all that part out once I started to
  15521. see how "hairy" the whole thing could get. 
  15522.  
  15523. I've only slightly been following the discussion since ( We're getting 
  15524. ready for a NIH site visit concerning our grant renewal! ) but I see 
  15525. it is even worse than I expected. 
  15526.  
  15527. I DO like the idea of "try  e_type, e_val:" as possibly the best 
  15528. backwards compatible syntax. They REALLY SHOULD be local names. 
  15529.  
  15530. You guys work it all out, and wake me when you're finished!
  15531.  
  15532. - Steve
  15533. 
  15534. 
  15535. Replied: Sat, 07 Mar 1992 00:29:25 +0100
  15536. Replied: peter@robots.oxford.ac.uk
  15537. Replied: python-list@cwi.nl
  15538. Received: by charon.cwi.nl with SMTP; Fri, 6 Mar 1992 22:03:31 +0100
  15539. Received: from prg.oxford.ac.uk by eros.uknet.ac.uk via JANET with NIFTP (PP) 
  15540.           id <3656-0@eros.uknet.ac.uk>; Fri, 6 Mar 1992 20:38:32 +0000
  15541. Received: from lucrece.robots (lucrece-gate.robots) by prg.oxford.ac.uk 
  15542.           id AA17075; Fri, 6 Mar 92 20:37:45 GMT
  15543. Received: from robots.ox.ac.uk (miranda.robots) 
  15544.           by uk.ac.oxford.robots (4.1/Robots 1.2m (20-Jun-90)) id AA22086;
  15545.           Fri, 6 Mar 92 20:35:54 GMT
  15546. Received: by robots.ox.ac.uk (4.1/robots.remoteV2.0) id AA04776;
  15547.           Fri, 6 Mar 92 20:36:11 GMT
  15548. Date: Fri, 6 Mar 92 20:36:11 GMT
  15549. From: peter@robots.oxford.ac.uk
  15550. Message-Id: <9203062036.AA04776@miranda.robots.ox.ac.uk>
  15551. To: python-list@cwi.nl
  15552. Subject: Re: sys.exc_type etc.
  15553.  
  15554. The following message I sent a few days ago has just bounced back.  It's
  15555. strange, but I have been receiving mail from Tim (tim@ksr.com) (through this
  15556. mailing list) about it.  So for the benefit of those that haven't read it,
  15557. here it is:
  15558.  
  15559. Subject: Re: sys.exc_type etc.
  15560.  
  15561. I was wondering whether the problem of the nested exceptions could be
  15562. handled the way you let the interpreter handles it.  Guido says,
  15563.     (Note that internally in the interpreter, there is no confusion
  15564.     between exceptions occurring in nested pieces of code -- it keeps them
  15565.     on its internal stack.)
  15566. As you pop this internal stack would it be possible to restore the exeception
  15567. type in sys.exc_type?
  15568.  
  15569.         Just a thought....,
  15570.             Pete. :-)
  15571. 
  15572. 
  15573. Received: by charon.cwi.nl with SMTP; Sat, 7 Mar 1992 00:29:26 +0100
  15574. Received: by voorn.cwi.nl with SMTP; Fri, 6 Mar 1992 23:29:24 GMT
  15575. Message-Id: <9203062329.AA02759@voorn.cwi.nl>
  15576. To: peter@robots.oxford.ac.uk
  15577. Cc: python-list@cwi.nl
  15578. Subject: bouncing messages on the python list
  15579. In-Reply-To: Your message of "Fri, 06 Mar 1992 20:36:11 MET."
  15580.              <9203062036.AA04776@miranda.robots.ox.ac.uk> 
  15581. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  15582. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  15583. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  15584. Date: Sat, 07 Mar 1992 00:29:23 +0100
  15585. Sender: Guido.van.Rossum@cwi.nl
  15586.  
  15587. >The following message I sent a few days ago has just bounced back.
  15588.  
  15589. I got a similar message from an earlier post.  Apparently there's one
  15590. address on the list whose host has disappeared.  I asked our sysadmin
  15591. and he said there's a bug in sendmail that loses the destination that
  15592. causes the error.  If the problem persists, I'll take the offending
  15593. person off the list until he complains.  Reposting does not appear to
  15594. be necessary.
  15595.  
  15596. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  15597. [I'm still thinking about the exception handler syntax...]
  15598. 
  15599. 
  15600. Received: by charon.cwi.nl with SMTP; Wed, 11 Mar 1992 19:32:33 +0100
  15601. Received: from prg.oxford.ac.uk by eros.uknet.ac.uk via JANET with NIFTP (PP) 
  15602.           id <28126-0@eros.uknet.ac.uk>; Wed, 11 Mar 1992 17:00:56 +0000
  15603. Received: from lucrece.robots (lucrece-gate.robots) by prg.oxford.ac.uk 
  15604.           id AA19099; Wed, 11 Mar 92 17:00:05 GMT
  15605. Received: by uk.ac.oxford.robots (4.1/Robots 1.2m (20-Jun-90)) id AA08997;
  15606.           Wed, 11 Mar 92 16:57:58 GMT
  15607. Date: Wed, 11 Mar 92 16:57:58 GMT
  15608. From: peter@robots.oxford.ac.uk (Peter Ho)
  15609. Message-Id: <9203111657.AA08997@uk.ac.oxford.robots>
  15610. To: python-list@cwi.nl
  15611. Subject: Re: sys.exc_type etc
  15612.  
  15613. I do agree with Tim that if we had
  15614.     try exc_type, exc_val:
  15615. the variables would clutter try block.  However, I think there usefulness
  15616. might outweigh it.  I agree with the suggestion made by Steve (sorry if I
  15617. got your name wrong), that these values would have to be local, so that only
  15618. visible in that try .. except .. finally .. block.  Would it be possible?
  15619.  
  15620.     Whilst we're on this point, Guido why didn't you make the exception
  15621. value variable in 'except' local to only that exception (like modula-3)?  You
  15622. would remove the problem that Tim pointed out of clobbering an exception
  15623. name.  I would be happy to see an example which finds this non-locality of
  15624. the exception values useful :-).
  15625.  
  15626.     Going back to those examples given by Tim:
  15627. >  | try:
  15628. >  |     print kidding
  15629. >  | except NameError:
  15630. >  |     try: 1/0
  15631. >  |     except:
  15632. >  |         print 'sys.exc_type should certainly be a zero-divide here'
  15633. >  |     # but what should sys.exc_type be here?
  15634. >  |     [tim sez NameError]
  15635. >  | ...
  15636. >  |
  15637. >  | elist = (None, NameError)
  15638. >  |
  15639. >  | def absurd(i):
  15640. >  |     try:
  15641. >  |         try:
  15642. >  |             1/0
  15643. >  |         except DivideByZeroError:  # meant ZeroDivisionError
  15644. >  |             print 'caught an exception that doesn\'t exist!'
  15645. >  |     except elist[i], val:
  15646. >  |         print 'caught the', elist[i], 'error on', val
  15647. >  |
  15648. >  | absurd(2)
  15649.  
  15650. I am coming to the belief that in both cases the value of sys.exc_type is
  15651. correct in each case.  If you have an error in your exception handler (or
  15652. before you enter the handler in the second example) then you should be told
  15653. about it.  What puzzles me though is the second example, what should happen
  15654. to sys.exc_type and exception reporting from Python, what about having a
  15655. list returned in sys.exc_type?
  15656.  
  15657.  
  15658.         Pete. 8-)
  15659. 
  15660. 
  15661. Received: by charon.cwi.nl with SMTP; Sun, 22 Mar 1992 22:16:04 +0100
  15662. Received: from pace1.cts.mtu.edu.pace by mtu.edu (4.1/SMI-4.1)
  15663.     id AA16207; Sun, 22 Mar 92 16:15:11 EST
  15664. Date: Sun, 22 Mar 92 16:15:11 EST
  15665. From: asbarnes@mtu.edu (ALAN BARNES)
  15666. Message-Id: <9203222115.AA16207@mtu.edu>
  15667. To: python-list@cwi.nl
  15668.  
  15669. Hi, this is my first post to the list so bear with me....I am trying to write
  15670. a simple finger server just to see if I can do it.  I basically have it working
  15671. except for a few things.  First off, it sends the passwd field.  I plan on
  15672. fixing this.  Another thing is that it is formatted terribly right now.  It just
  15673. spits the information out with no indication as to what it is.  And my real
  15674. problem is this:  I can connect to the port I am running this on.  I type in a
  15675. user name and it gives me the information for that user.  And then it closes
  15676. the connection.  But what I want to do is only kill the connection of the 
  15677. person who called.  I can't seem to figure out how to do this.  So, my little
  15678. finger program will only finger one person then quit.  Any suggestions will be
  15679. appreciated.  Thanks...I am including a copy of my program...
  15680.  
  15681.  
  15682.  
  15683.  
  15684. #this is a finger program I am writing in the python language
  15685. #it isn't working so hot.....
  15686. #
  15687.  
  15688. import string
  15689. from time import sleep
  15690. import sys
  15691. from socket import *
  15692. from pwd import getpwnam
  15693.  
  15694. PORT = 45678
  15695. SIZE = 10
  15696.  
  15697. def main():                 #recognize this?
  15698.     if len(sys.argv) > 1:
  15699.         PORT = int(eval(sys.argv[1]))
  15700.     else:
  15701.         PORT = 45678
  15702.     s = socket(AF_INET, SOCK_STREAM)
  15703.     s.bind('', PORT)
  15704.     s.listen(3)
  15705.     conn, (remotehost, remoteport) = s.accept()
  15706.     print 'Connection from ', remotehost, ' established.'
  15707.     data = conn.recv(SIZE)
  15708.     print len(data)
  15709.     data = data[:-2]
  15710.     field = getpwnam(data)
  15711.     for x in range(len(field)):
  15712.         conn.send(string.zfill(field[x], 1))
  15713.         conn.send('\n')
  15714.     conn.send('\n')
  15715.     conn.close()
  15716.  
  15717. main()
  15718. 
  15719. 
  15720. Received: by charon.cwi.nl with SMTP; Mon, 23 Mar 1992 10:09:34 +0100
  15721. Received: by schelvis.cwi.nl with SMTP; Mon, 23 Mar 1992 09:09:31 GMT
  15722. Message-Id: <9203230909.AA12777@schelvis.cwi.nl>
  15723. To: asbarnes@mtu.edu (ALAN BARNES)
  15724. Cc: python-list@cwi.nl
  15725. Subject: 
  15726. In-Reply-To: Message by asbarnes@mtu.edu (ALAN BARNES) ,
  15727.          Sun, 22 Mar 92 16:15:11 EST , <9203222115.AA16207@mtu.edu> 
  15728. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  15729. Phone: +31 20 5924098(work), +31 20 6160335(home)
  15730. X-Last-Band-Seen: Wild Pumpkins at Midnight (Buurvrouw, 15-3)
  15731. X-Mini-Review: Great guitarband, even acoustic...
  15732. Date: Mon, 23 Mar 1992 10:09:30 +0100
  15733. From: Jack Jansen <Jack.Jansen@cwi.nl>
  15734.  
  15735. That's fairly simple. The reason for having separate 'listen' and
  15736. 'accept' calls in the bsd socket stuff is to do exactly that. Here's
  15737. the fixed code:
  15738.  
  15739. #this is a finger program I am writing in the python language
  15740. #it isn't working so hot.....
  15741. #
  15742.  
  15743. import string
  15744. from time import sleep
  15745. import sys
  15746. from socket import *
  15747. from pwd import getpwnam
  15748.  
  15749. PORT = 45678
  15750. SIZE = 10
  15751.  
  15752. def main():                 #recognize this?
  15753.     if len(sys.argv) > 1:
  15754.         PORT = int(eval(sys.argv[1]))
  15755.     else:
  15756.         PORT = 45678
  15757.     s = socket(AF_INET, SOCK_STREAM)
  15758.     s.bind('', PORT)
  15759.     s.listen(3)
  15760.     while 1:
  15761.         conn, (remotehost, remoteport) = s.accept()
  15762.         print 'Connection from ', remotehost, ' established.'
  15763.         data = conn.recv(SIZE)
  15764.         print len(data)
  15765.         data = data[:-2]
  15766.         field = getpwnam(data)
  15767.         for x in range(len(field)):
  15768.             conn.send(string.zfill(field[x], 1))
  15769.             conn.send('\n')
  15770.         conn.send('\n')
  15771.         conn.close()
  15772.  
  15773. main()
  15774.  
  15775. --
  15776. Jack Jansen        | If I can't dance I don't want to be part of
  15777. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  15778. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  15779. 
  15780. 
  15781. Received: by charon.cwi.nl with SMTP; Thu, 26 Mar 1992 21:12:30 +0100
  15782. Received: by meermin.cwi.nl with SMTP; Thu, 26 Mar 1992 20:12:22 GMT
  15783. Received: by relay.CDNnet.CA (4.1/1.14)
  15784.     id AA18486; Thu, 26 Mar 92 12:11:20 PST
  15785. Date: 26 Mar 92 12:07 -0800
  15786. From: John Buchanan <juancho@cs.ubc.ca>
  15787. To: <python-list@cwi.nl>
  15788. Reply-To: <juancho@cs.ubc.ca>
  15789. Message-Id: <1852*juancho@cs.ubc.ca>
  15790. Subject: Embeding python
  15791.  
  15792. In the abstract for the python tutorial there is a very interesting
  15793. sentence, it reads:
  15794.  
  15795.     Python is also suitable as an extension language for highly
  15796.     customizable C applicatons such as editors or window managers.
  15797.  
  15798. Unfortunately there is no discussion of this (Not that I could find) in
  15799. the documentation that came with 
  15800.  
  15801.     Python 0.9.4 alpha (Jan 30 1992).
  15802.  
  15803.  
  15804. Does this sentence mean that it is possible to embed python into a C
  15805. program?  If this is the case could some one send me some pointers as to
  15806. how to accomplish this.
  15807.  
  15808.     thanks 
  15809.  
  15810.     juancho
  15811. 
  15812. 
  15813. Received: by charon.cwi.nl with SMTP; Thu, 26 Mar 1992 21:32:15 +0100
  15814. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa18990;
  15815.           26 Mar 92 15:31 EST
  15816. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  15817.     id AA09390; Thu, 26 Mar 92 15:30:39 EST
  15818. Date: Thu, 26 Mar 92 15:30:39 EST
  15819. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  15820. Message-Id: <9203262030.AA09390@aemsun.med.Virginia.EDU>
  15821. To: juancho@cs.ubc.ca
  15822. Subject: Re:  Embeding python
  15823. Cc: python-list@cwi.nl
  15824.  
  15825. juancho@cs.ucb.ca writes:
  15826. > In the abstract for the python tutorial there is a very interesting
  15827. > sentence, it reads:
  15828. >     Python is also suitable as an extension language for highly
  15829. >     customizable C applicatons such as editors or window managers.
  15830. > Unfortunately there is no discussion of this (Not that I could find) in
  15831. > the documentation that came with 
  15832. >     Python 0.9.4 alpha (Jan 30 1992).
  15833. > Does this sentence mean that it is possible to embed python into a C
  15834. > program?  If this is the case could some one send me some pointers as to
  15835. > how to accomplish this.
  15836.  
  15837. YES.
  15838.  
  15839. HOW:
  15840.  First: read "misc/EXTENDING" . 
  15841.  Next: Start reading the sources. 
  15842.  
  15843.  ( Is there a step in between I'm missing, Guido ? ) 
  15844.  
  15845. Perhaps Steve Miale ( here at UVA EE|CS ) can give us a progress report
  15846. on what they are doing ? (hint,hint!) 
  15847.  
  15848. - Steve Majewski  sdm7g@Virginia.EDU 
  15849.  
  15850.  
  15851. 
  15852. 
  15853. Replied: Fri, 27 Mar 1992 14:26:40 +0100
  15854. Replied: "python-list@cwi.nl "
  15855. Received: by charon.cwi.nl with SMTP; Thu, 26 Mar 1992 21:50:07 +0100
  15856. Received: by relay.CDNnet.CA (4.1/1.14)
  15857.     id AA19151; Thu, 26 Mar 92 12:49:56 PST
  15858. Date: 26 Mar 92 12:47 -0800
  15859. From: John Buchanan <juancho@cs.ubc.ca>
  15860. To: "Steven D. Majewski" <sdm7g@aemsun.med.virginia.edu>
  15861. Cc: <python-list@cwi.nl>
  15862. In-Reply-To: <9203262030.AA09390@aemsun.med.Virginia.EDU>
  15863. Message-Id: <1853*juancho@cs.ubc.ca>
  15864. Subject: Re:  Embeding python
  15865.  
  15866.  
  15867.  
  15868. I read misc/EXTENDING (quickly) and it seems as though it is possible but
  15869. not that straight forward.  What I was looking for was something that looks
  15870. as follows.
  15871.  
  15872. int load_python_program(char *pathname_to_python_program)
  15873.     /* Or some function to initialize and load python */
  15874.  
  15875. int run_python_program_with_arguments(arg1 arg2 arg3.....argn)
  15876.     /* call some procedure in the loaded program */
  15877.  
  15878. This may be far to simple, but I hope that something like this is possible.
  15879.  
  15880. We wish to use python in this manner since we have an application which
  15881. we have already implemented and we wish to add a programmable extension
  15882. to it.  
  15883.  
  15884.         juancho
  15885.  
  15886.  
  15887.  
  15888. 
  15889. 
  15890. Received: by charon.cwi.nl with SMTP; Fri, 27 Mar 1992 10:27:01 +0100
  15891. Received: by schelvis.cwi.nl with SMTP; Fri, 27 Mar 1992 09:27:00 GMT
  15892. Message-Id: <9203270927.AA22012@schelvis.cwi.nl>
  15893. To: John Buchanan <juancho@cs.ubc.ca>
  15894. Cc: "Steven D. Majewski" <sdm7g@aemsun.med.virginia.edu>, python-list@cwi.nl
  15895. Subject: Re: Embeding python 
  15896. In-Reply-To: Message by John Buchanan <juancho@cs.ubc.ca> ,
  15897.          26 Mar 92 12:47 -0800 , <1853*juancho@cs.ubc.ca> 
  15898. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  15899. Phone: +31 20 5924098(work), +31 20 6160335(home)
  15900. X-Last-Band-Seen: Wild Pumpkins at Midnight (Buurvrouw, 15-3)
  15901. X-Mini-Review: Great guitarband, even acoustic...
  15902. Date: Fri, 27 Mar 1992 10:26:59 +0100
  15903. From: Jack Jansen <Jack.Jansen@cwi.nl>
  15904.  
  15905. Hmm... Guido'll have something to say about it when he comes in, no
  15906. doubt, but personally I think that the referenced sentence is probably
  15907. a bit too optimistic.
  15908.  
  15909. What you can do is turn your C program into a module, embed it in a
  15910. tiny python program and call python modules from your C code.
  15911.  
  15912. The python code is real simple. Assuming you call your C module 'foo',
  15913. you do something like
  15914.  
  15915.   import foo
  15916.  
  15917.   def .....    # Define all python functions here, or in other modules,
  15918.           # whatever you want...
  15919.  
  15920.   foo.callmymain()
  15921.  
  15922. Turning your program into a module isn't too much work: rename main()
  15923. to my_main() and write a wrapper module (as described in
  15924. misc/EXTENDING) that calls your main program. You can then use
  15925. dictlookup() (I think) to find python objects and call_object() to
  15926. call those objects from your C code.
  15927.  
  15928. Unless you have the latest python with the dynamic loading facility
  15929. you then have to create a new python binary that includes your new
  15930. module (and your old application program as well, of course). With
  15931. dynamic loading things are a bit simpler, since you can just leave a
  15932. partially linked 'foomodule.o' around somewhere in the $PYTHONPATH,
  15933. and an 'import foo' will result in the module being dynamically linked
  15934. into the running python interpreter.
  15935.  
  15936. Maybe Guido can provide us with a complete sceleton example?
  15937. --
  15938. Jack Jansen        | If I can't dance I don't want to be part of
  15939. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  15940. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  15941. 
  15942. 
  15943. Received: by charon.cwi.nl with SMTP; Fri, 27 Mar 1992 14:26:43 +0100
  15944. Received: by voorn.cwi.nl with SMTP; Fri, 27 Mar 1992 13:26:42 GMT
  15945. Message-Id: <9203271326.AA15685@voorn.cwi.nl>
  15946. To: python-list@cwi.nl
  15947. Subject: Re: Embedding python 
  15948. In-Reply-To: Your message of "26 Mar 1992 12:47:00 MET."
  15949.              <1853*juancho@cs.ubc.ca> 
  15950. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  15951. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  15952. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  15953. Date: Fri, 27 Mar 1992 14:26:41 +0100
  15954. Sender: Guido.van.Rossum@cwi.nl
  15955.  
  15956. Embedding Python in C is possible, but misc/EXTENDING isn't the whole
  15957. answer.  It only describes how to extend Python with code written in
  15958. C, but you still have to have a "main program" written in Python.
  15959.  
  15960. By definition, "embedding" means doing it the other way around, like
  15961. John writes:
  15962.  
  15963. >What I was looking for was something that looks as follows.
  15964. >
  15965. >int load_python_program(char *pathname_to_python_program)
  15966. >    /* Or some function to initialize and load python */
  15967. >
  15968. >int run_python_program_with_arguments(arg1 arg2 arg3.....argn)
  15969. >    /* call some procedure in the loaded program */
  15970.  
  15971. Such a simple interface currently does not exist, but it shouldn't be
  15972. too hard to create it, or something similar.
  15973.  
  15974. I hope you're a bit adventurous; then you can read pythonmain.c and
  15975. figure out how to do it.  Basically, you'll need to move all functions
  15976. from that file elsewhere, and add calls to initpython() (at least) and
  15977. setpythonpath() (probably) and setpythonargv() (perhaps) to your own
  15978. main program.  You can then call routines like run_command(string) or
  15979. run(FILEpointer, filename) to execute Python code as you wish.  There
  15980. are other run_*() functions that provide more flexibility, like
  15981. choosing which environment to use.
  15982.  
  15983. You'll probably find that the Python code run in this way needs access
  15984. to funactionality defined in your application, and for *that* you'll
  15985. need to do everything described in misc/EXTENDING.
  15986.  
  15987. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  15988. "What's so unpleasant about being drunk?"
  15989. "Go ask a glass of water!"
  15990. 
  15991. 
  15992. Replied: Mon, 30 Mar 1992 11:05:12 +0200
  15993. Replied: "John Buchanan <juancho@cs.ubc.ca> "
  15994. Received: by charon.cwi.nl with SMTP; Fri, 27 Mar 1992 19:38:08 +0100
  15995. Received: by relay.CDNnet.CA (4.1/1.14)
  15996.     id AA00598; Fri, 27 Mar 92 10:37:53 PST
  15997. Date: 27 Mar 92  9:58 -0800
  15998. From: John Buchanan <juancho@cs.ubc.ca>
  15999. To: <Guido.van.Rossum@cwi.nl>
  16000. Cc: <python-list@cwi.nl>
  16001. In-Reply-To: <9203271326.AA15685@voorn.cwi.nl>
  16002. Message-Id: <1855*juancho@cs.ubc.ca>
  16003. Subject: Re: Embedding python 
  16004.  
  16005.  
  16006. >Embedding Python in C is possible, but misc/EXTENDING isn't the whole
  16007. >answer.  It only describes how to extend Python with code written in
  16008. >C, but you still have to have a "main program" written in Python.
  16009. >
  16010. >By definition, "embedding" means doing it the other way around, like
  16011. >John writes:
  16012. >
  16013. >>What I was looking for was something that looks as follows.
  16014. >>
  16015. >>int load_python_program(char *pathname_to_python_program)
  16016. >>       /* Or some function to initialize and load python */
  16017. >>
  16018. >>int run_python_program_with_arguments(arg1 arg2 arg3.....argn)
  16019. >>       /* call some procedure in the loaded program */
  16020. >
  16021. >
  16022. >
  16023. >Such a simple interface currently does not exist, but it shouldn't be
  16024. >too hard to create it, or something similar.
  16025. >
  16026. >I hope you're a bit adventurous; then you can read pythonmain.c and
  16027. >figure out how to do it.  Basically, you'll need to move all functions
  16028. >from that file elsewhere, and add calls to initpython() (at least) and
  16029. >setpythonpath() (probably) and setpythonargv() (perhaps) to your own
  16030. >main program.  You can then call routines like run_command(string) or
  16031. >run(FILEpointer, filename) to execute Python code as you wish.  There
  16032. >are other run_*() functions that provide more flexibility, like
  16033. >choosing which environment to use.
  16034.  
  16035.  
  16036.  
  16037. I took a quick look at pythonmain.c and I have a couple of questions and or
  16038. suggestions.  
  16039.  
  16040. main() seems simple enough, so I would like to suggest the following.
  16041.  
  16042. rewrite and rename main() as follows
  16043.  
  16044. initialize_python(int argc,char argv[])
  16045.  
  16046.  
  16047. write a separate module for python of which looks like this
  16048.  
  16049. the_real_python_main.c
  16050.  
  16051. main(int argc, char *argv[])
  16052.     {
  16053.     initialize_python(argc,argv);
  16054.     }
  16055.  
  16056. Now compile everything but the_real_python_main.c as a library.
  16057. compile and link python.
  16058.  
  16059. Now to call from a c program we do the following
  16060.  
  16061. initialize_python(5 ,argv);
  16062.  
  16063.  
  16064.  
  16065. Now I realize that this is a simple approach and is probably not going 
  16066. to work as stated but I think that the idea of making the bulk of python
  16067. into a library and then having a simple front end for it is the way to go.
  16068. This allows python to be embedded quite easily and would not result in
  16069. incompatible versions of python running around.
  16070.  
  16071.     juancho
  16072. 
  16073. 
  16074. Received: by charon.cwi.nl with SMTP; Fri, 27 Mar 1992 20:14:41 +0100
  16075. Received: from pace1.cts.mtu.edu.pace by mtu.edu (4.1/SMI-4.1)
  16076.     id AA07693; Fri, 27 Mar 92 14:13:28 EST
  16077. From: asbarnes@mtu.edu (ALAN BARNES)
  16078. Message-Id: <9203271913.AA07693@mtu.edu>
  16079. Subject: system and open
  16080. To: python-list@cwi.nl
  16081. Date: Fri, 27 Mar 92 14:13:39 EST
  16082. X-Mailer: ELM [version 2.3 PL11]
  16083.  
  16084. Ok, a couple of questions.  Where is write and related functions located?
  16085. They aren't in built-in with open.  I cannot find close either.  I want to 
  16086. write the output of something to a file but cannot figure out where these 
  16087. commands are located.  They are only mentioned breifly in the docs I have which
  16088. I am assuming are the current ones.  Also, is there any way to get the output
  16089. of a system command assigned to a variable?  e.g. list = system('ls').  I want
  16090. to do something like this also but don't know if I would be able to.  Any ideas
  16091. are appreciated.  Thanks...
  16092.  
  16093. -- 
  16094.  
  16095.             +------------------------------------------------+
  16096.             |              /--------------------------\      |
  16097.             |             |  Bart Got an Erection      |     |
  16098.             | |\/\/\/|   /   Bart Didn't Use Protection \    |
  16099.             | |      |   |   Bart Got an Infection !    |    |
  16100.             | | (o)(o)   \_   _________________________/     |
  16101.             | c      _)    | /                               |
  16102.             |  | '___|    <_/     Don't be tough             |
  16103.             |  |   /              Don't be silly             |
  16104.             |  /____\             Wear a condom              |
  16105.             | /      \            on your Willy !            |
  16106.             +------------------------------------------------+
  16107.             |  M       asbarnes@mtu.edu                      |
  16108.             |   T      asbarnes@symmetry.cs.mtu.edu          |
  16109.             |    U     asbarnes@mtus5.cts.mtu.edu            |
  16110.             |            A.K.A.   The Artful Dodger          |
  16111.             *_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-*
  16112.  
  16113. 
  16114. 
  16115. Received: by charon.cwi.nl with SMTP; Fri, 27 Mar 1992 21:15:18 +0100
  16116. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa14162;
  16117.           27 Mar 92 15:10 EST
  16118. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  16119.     id AA11258; Fri, 27 Mar 92 15:09:54 EST
  16120. Date: Fri, 27 Mar 92 15:09:54 EST
  16121. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  16122. Message-Id: <9203272009.AA11258@aemsun.med.Virginia.EDU>
  16123. To: asbarnes@mtu.edu
  16124. Subject: Re:  system and open
  16125. Cc: python-list@cwi.nl
  16126.  
  16127.  
  16128. >From asbarnes@mtu.edu>
  16129. > Ok, a couple of questions.  Where is write and related functions located?
  16130. > They aren't in built-in with open.  I cannot find close either. 
  16131.  
  16132. write & close are methods of file objects, so they are built-in, even 
  16133. though you don't see them in the scope of 'builtin' .
  16134.  
  16135.  
  16136. >>>import builtin 
  16137. >>>dir(builtin) 
  16138. ['AttributeError', 'EOFError',
  16139. 'IOError', 'ImportError', 'IndexError', 'KeyError',
  16140. 'KeyboardInterrupt', 'MemoryError', 'NameError', 'None',
  16141. 'OverflowError', 'RuntimeError', 'SyntaxError', 'SystemError',
  16142. 'TypeError', 'ValueError', 'ZeroDivisionError', 'abs', 'apply', 'chr',
  16143. 'dir', 'divmod', 'eval', 'exec', 'float', 'hex', 'input', 'int',
  16144. 'len', 'long', 'max', 'min', 'oct', 'open', 'ord', 'pow', 'range',
  16145. 'raw_input', 'reload', 'type']
  16146.  
  16147. >>>f = open( '/dev/tty', 'r' )
  16148. >>>f
  16149. <open file '/dev/tty', mode 'r'>
  16150.  
  16151. >>>f.__methods__
  16152. ['close', 'flush', 'isatty', 'read', 'readline', 'readlines', 
  16153. 'seek', 'tell', 'write']
  16154.  
  16155. >>>f.write
  16156. <built-in method 'write' of some file object>
  16157. >>>f.close
  16158. <built-in method 'close' of some file object>
  16159.  
  16160.  
  16161.  
  16162. > Also, is there any way to get the output
  16163. > of a system command assigned to a variable?
  16164. > e.g. list = system('ls').  
  16165.  
  16166.  
  16167. >>>import posix
  16168. >>>ls = posix.popen( 'ls -l', 'r' ).readlines()
  16169. >>>ls 
  16170. ['total 75\012', 
  16171. '-rw-r--r-- 1 sdm7g 2401 Mar 27 14:55 #Re11196#\012',
  16172. '-rw------- 1 zhu 34 Mar 19 19:15 MTda01454\012', 
  16173. '-rw-r--r-- 1 zhu 34 Mar 27 11:45 MTda10945\012', 
  16174. '-rw------- 1 zhu 5530 Mar 19 19:15 MTvia01454\012', 
  16175. '-rw------- 1 sdm7g 2270 Mar 27 14:44 Re11196\012',
  16176. '-rw------- 1 zhu 0 Mar 19 19:15 Text1454.0\012', 
  16177.  .
  16178.  .
  16179.  .
  16180. '-rw------- 1 zhu 0 Mar 27 11:41 tty.txt.a10942\012']
  16181.  
  16182. >
  16183. > .sig deleted
  16184. >
  16185.  
  16186. My condolences.
  16187. That's about the WORST case of .sig virus I've seen around! :-)
  16188.  
  16189. - Steve Majewski     sdm7g@Virginia.EDU
  16190. 
  16191. 
  16192. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  16193.     id AA11749 (5.65b/2.9/CWI-Amsterdam); Mon, 6 Apr 1992 16:21:06 +0200
  16194. Received: by voorn.cwi.nl with SMTP; Mon, 6 Apr 1992 14:21:05 GMT
  16195. Message-Id: <9204061421.AA20700@voorn.cwi.nl>
  16196. To: python-list@cwi.nl
  16197. Subject: Python release 0.9.6 is here!
  16198. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  16199. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  16200. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  16201. Date: Mon, 06 Apr 1992 16:21:04 +0200
  16202. Sender: Guido.van.Rossum@cwi.nl
  16203.  
  16204. Hi folks,
  16205.  
  16206. I've finally finished the work on Python release 0.9.6.
  16207.  
  16208. Below are the README and the NEWS files from the distribution.
  16209.  
  16210. Ftp accessibility is through ftp.cwi.nl:/pub (Europe) and
  16211. wuarchive.wustl.edu:/pub (America).
  16212.  
  16213. You are encouraged to port this to your system and report any problems
  16214. you have.  If bug reports warrant it, I'll bring out a maintenance
  16215. release 0.9.7 within a few weeks.  But don't hold your breath, it may
  16216. never happen: this release is pretty rock solid as far as real bugs
  16217. are concerned, although there could be little portability problems
  16218. (the range of machines I have access to has shrunk considerably -- no
  16219. more "pure 4.3 BSD" machines around here :-( ).
  16220.  
  16221. ==> CWI readers please note: I've installed this version in
  16222.     /usr/local, but it may take a night to reach all file servers.
  16223.  
  16224. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  16225. "Strange women, laying in ponds, distributing swords, is no basis for a
  16226. system of government."
  16227.  
  16228. ****************************** NEWS ************************************
  16229.  
  16230. ==============================
  16231. ==> NEWS FOR RELEASE 0.9.6 <==
  16232. ==============================
  16233.  
  16234. Misc news in 0.9.6:
  16235. - Restructured the misc subdirectory
  16236. - Reference manual completed, library manual much extended (with indexes!)
  16237. - the GNU Readline library is now distributed standard with Python
  16238. - the script "../demo/scripts/classfix.py" fixes Python modules using old
  16239.   class syntax
  16240. - Emacs python-mode.el (was python.el) vastly improved (thanks, Tim!)
  16241. - Because of the GNU copyleft business I am not using the GNU regular
  16242.   expression implementation but a free re-implementation by Tatu Ylonen
  16243.   that recently appeared in comp.sources.misc (Bravo, Tatu!)
  16244.  
  16245. New features in 0.9.6:
  16246. - stricter try stmt syntax: cannot mix except and finally clauses on 1 try
  16247. - New module 'os' supplants modules 'mac' and 'posix' for most cases;
  16248.   module 'path' is replaced by 'os.path'
  16249. - os.path.split() return value differs from that of old path.split()
  16250. - sys.exc_type, sys.exc_value, sys.exc_traceback are set to the exception
  16251.   currently being handled
  16252. - sys.last_type, sys.last_value, sys.last_traceback remember last unhandled
  16253.   exception
  16254. - New function string.expandtabs() expands tabs in a string
  16255. - Added times() interface to posix (user & sys time of process & children)
  16256. - Added uname() interface to posix (returns OS type, hostname, etc.)
  16257. - New built-in function execfile() is like exec() but from a file
  16258. - Functions exec() and eval() are less picky about whitespace/newlines
  16259. - New built-in functions getattr() and setattr() access arbitrary attributes
  16260. - More generic argument handling in built-in functions (see "./EXTENDING")
  16261. - Dynamic loading of modules written in C or C++ (see "./DYNLOAD")
  16262. - Division and modulo for long and plain integers with negative operands
  16263.   have changed; a/b is now floor(float(a)/float(b)) and a%b is defined
  16264.   as a-(a/b)*b.  So now the outcome of divmod(a,b) is the same as
  16265.   (a/b, a%b) for integers.  For floats, % is also changed, but of course
  16266.   / is unchanged, and divmod(x,y) does not yield (x/y, x%y)...
  16267. - A function with explicit variable-length argument list can be declared
  16268.   like this: def f(*args): ...; or even like this: def f(a, b, *rest): ...
  16269. - Code tracing and profiling features have been added, and two source
  16270.   code debuggers are provided in the library (pdb.py, tty-oriented,
  16271.   and wdb, window-oriented); you can now step through Python programs!
  16272.   See sys.settrace() and sys.setprofile(), and "../lib/pdb.doc"
  16273. - '==' is now the only equality operator; "../demo/scripts/eqfix.py" is
  16274.   a script that fixes old Python modules
  16275. - Plain integer right shift now uses sign extension
  16276. - Long integer shift/mask operations now simulate 2's complement
  16277.   to give more useful results for negative operands
  16278. - Changed/added range checks for long/plain integer shifts
  16279. - Options found after "-c command" are now passed to the command in sys.argv
  16280.   (note subtle incompatiblity with "python -c command -- -options"!)
  16281. - Module stdwin is better protected against touching objects after they've
  16282.   been closed; menus can now also be closed explicitly
  16283. - Stdwin now uses its own exception (stdwin.error)
  16284.  
  16285. New features in 0.9.5 (released as Macintosh application only, 2 Jan 1992):
  16286. - dictionary objects can now be compared properly; e.g., {}=={} is true
  16287. - new exception SystemExit causes termination if not caught;
  16288.   it is raised by sys.exit() so that 'finally' clauses can clean up,
  16289.   and it may even be caught.  It does work interactively!
  16290. - new module "regex" implements GNU Emacs style regular expressions;
  16291.   module "regexp" is rewritten in Python for backward compatibility
  16292. - formal parameter lists may contain trailing commas
  16293.  
  16294. Bugs fixed in 0.9.6:
  16295. - assigning to or deleting a list item with a negative index dumped core
  16296. - divmod(-10L,5L) returned (-3L, 5L) instead of (-2L, 0L)
  16297.  
  16298. Bugs fixed in 0.9.5:
  16299. - masking operations involving negative long integers gave wrong results
  16300.  
  16301. ****************************** README **********************************
  16302.  
  16303. Python source distribution, release 0.9.6
  16304. -----------------------------------------
  16305.  
  16306. ==> If you don't know yet what Python is: it's an interpreted,
  16307.     interactive, object-oriented programming language.  Read the file
  16308.     misc/BLURB for more information.
  16309.  
  16310. ==> If you want to start compiling right away (UNIX only): cd to the
  16311.     src subdirectory, *read* and *edit* the Makefile there, and run
  16312.     make.  But it's better to read misc/BUILD first.
  16313.  
  16314. ==> If you want to know what's new since release 0.9.4 (somehow,
  16315.     version 0.9.5 was never released): read misc/NEWS.  Older history
  16316.     is in misc/HOSTORY.
  16317.  
  16318. ==> See the copyright notice at the end of this file.
  16319.  
  16320. Distribution structure
  16321. ----------------------
  16322.  
  16323. Each subdirectory has a README file explaining its contents in more
  16324. detail.  The subdirectories at the top level are:
  16325.  
  16326. src/        C sources -- you build the Python binary here
  16327. lib/        Python sources for standard Python modules
  16328. demo/        Python sources for various demos of Python's abilities
  16329. doc/        LaTeX sources for the main documentation
  16330. misc/        UNIX man page, internal documentation, Emacs mode, etc.
  16331. readline/    Source for the GNU Readline library
  16332.  
  16333. Author
  16334. ------
  16335.  
  16336. Guido van Rossum
  16337. CWI, dept. CST
  16338. Kruislaan 413
  16339. 1098 SJ  Amsterdam
  16340. The Netherlands
  16341.  
  16342. E-mail: guido@cwi.nl
  16343.  
  16344. Ftp access
  16345. ----------
  16346.  
  16347. The latest Python source distribution can be ftp'ed from site
  16348. ftp.cwi.nl (IP address 192.16.184.180), directory /pub, file
  16349. python<version>.tar.Z.  You can also find PostScript of the main
  16350. Python documentation there, a Macintosh binary, and the latest stdwin
  16351. source distribution.  I try to upload copies of these files to
  16352. wuarchive.wustl.edu:/pub, to save on trans-atlantic traffic.  See also
  16353. misc/FTP.
  16354.  
  16355. Mailing list
  16356. ------------
  16357.  
  16358. There is a mailing list devoted to Python programming, bugs and
  16359. design.  To subscribe, send mail containing your real name and e-mail
  16360. address in Internet form to "python-list-request@cwi.nl".
  16361.  
  16362. Copyright Notice
  16363. ----------------
  16364.  
  16365. The Python source is copyrighted, but you can freely use and copy it
  16366. as long as you don't change or remove the copyright:
  16367.  
  16368. Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
  16369. Netherlands.
  16370.  
  16371.                         All Rights Reserved
  16372.  
  16373. Permission to use, copy, modify, and distribute this software and its 
  16374. documentation for any purpose and without fee is hereby granted, 
  16375. provided that the above copyright notice appear in all copies and that
  16376. both that copyright notice and this permission notice appear in 
  16377. supporting documentation, and that the names of Stichting Mathematisch
  16378. Centrum or CWI not be used in advertising or publicity pertaining to
  16379. distribution of the software without specific, written prior permission.
  16380.  
  16381. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16382. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16383. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  16384. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16385. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16386. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  16387. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16388.  
  16389. ************************************************************************
  16390. 
  16391. 
  16392. Received: from hopscotch.ksr.com by charon.cwi.nl with SMTP
  16393.     id AA26513 (5.65b/2.9/CWI-Amsterdam); Tue, 7 Apr 1992 05:43:19 +0200
  16394. Received: from ksr.com ([192.9.200.90]) by hopscotch.ksr.com with SMTP
  16395.     id AA03572; Mon, 6 Apr 1992 23:43:05 -0400
  16396. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  16397.     id AA06189; Mon, 6 Apr 92 23:45:53 EDT
  16398. Received: by kaos.ksr.com (4.0/KSR-2.0)
  16399.     id AA28100; Mon, 6 Apr 92 23:45:51 EDT
  16400. Message-Id: <9204070345.AA28100@kaos.ksr.com>
  16401. To: python-list@cwi.nl
  16402. Subject: (Emacs) python-mode.el patch, vers 1.06x -> 1.06
  16403. Date: Mon, 06 Apr 92 23:45:50 -0400
  16404. From: Tim Peters <tim@ksr.com>
  16405.  
  16406. Congratulations to Guido on another fine release!
  16407.  
  16408. If you're using the Emacs Python mode, the patch below will change the
  16409. experimental version (1.06x) distributed with the Python 0.9.6 release
  16410. to the "official" version (1.06).  Note that 1.06x and 1.06 both
  16411. *require* Python 0.9.6 for proper functioning (they rely on the nifty
  16412. new "execfile" builtin).
  16413.  
  16414. There are few visible changes from 1.05; in general, some things are
  16415. just slicker, and there are a few new user-settable variables to control
  16416. scrolling behavior and to name a temp directory.
  16417.  
  16418. One bugfix:  1.05 & 1.06x didn't realize that "while" loops can have an
  16419. "else" clause.  1.06 does.
  16420.  
  16421. One outstanding bug:  Sometimes when you're at the end of a file that
  16422. doesn't end with a newline, the indentation commands just don't work
  16423. right.  E.g., DEL may move back a single column when it should close a
  16424. block, or TAB may move to the wrong column.  Nobody has been able to
  16425. reproduce this reliably -- it seems to be a function of both the file
  16426. content and the history of inserts and deletes.  On three occasions when
  16427. this happened, I have definitely seen Emacs returning a wrong value for
  16428. the Elisp function `current-indentation', and this does appear to be the
  16429. underlying cause.  Since it a appears to be a flaky bug in some versions
  16430. of Emacs out there, there's not much I can do about it.  If it happens
  16431. to you, make sure your file ends with a newline and try again -- that's
  16432. fixed it every time for me.  And if you stumble into a reproducible case,
  16433. please send it on & I'll try to follow up on it.
  16434.  
  16435. have-fun!-ly y'rs  - tim
  16436.  
  16437. Tim Peters   Kendall Square Research Corp
  16438. tim@ksr.com,         ksr!tim@uunet.uu.net
  16439.  
  16440. Change log:
  16441.  
  16442. Mon Apr  6 23:14:18 1992  tim
  16443. version 1.06
  16444. beefed up py-shell docs
  16445.     point to emacs shell-mode docs
  16446.     warn against changing python's ps[12]
  16447. fixed set-up of syntax table to stop changing the syntax of the buffer
  16448.     py-mode is loaded from (probably only affects py-mode developers
  16449.     doing eval-current-buffer)
  16450. changed set-up of py-mode-map just to look more like the others
  16451. junked py-process-send-string-wait
  16452. rewrote py-execute-region, py-process-filter, py-append-to-process-buffer
  16453.     to add new scrolling features, and to use python's new execfile
  16454.     command
  16455.     new vrbl py-temp-directory
  16456.     new vrbl py-scroll-process-buffer
  16457.     new secret vrbl py-file-queue
  16458.     new helper py-make-temp-name
  16459.     new helper py-execute-file
  16460.     new helper py-delete-file-silently
  16461. new hair to try to make sure the temp files are deleted when emacs
  16462.     exits (in case buffer dies/gets_killed while files pending)
  16463.     new secret vrbl py-inherited-kill-emacs-hook
  16464.     new helper py-kill-emacs-hook
  16465. changed py-append-to-process-buffer to muck with shell-mode's "last
  16466.     input" markers so that C-c C-r and C-c C-o work intuitively
  16467.     after sending a region
  16468. added bizarre hack to py-process-filter -- major mysteries here, but
  16469.     emacs segfaults (etc) without this
  16470. added warning about trying to undo edits that occurred while process
  16471.     output is arriving -- this one's certainly Emacs's fault
  16472.     heck, maybe it's a feature!
  16473. changed py-shell to make the process buffer use python's syntax table
  16474. taught py-mark-block that 'while' loops can have an 'else' clause
  16475.  
  16476. Patch (1.06x -> 1.06):
  16477.  
  16478. *** python-mode.el    Mon Apr  6 23:10:28 1992
  16479. --- ../python-mode.el    Mon Apr  6 23:08:51 1992
  16480. ***************
  16481. *** 1,4 ****
  16482. ! ;;; Major mode for editing Python programs, version 1.06x
  16483.   ;; by: Michael A. Guravage
  16484.   ;;     Guido van Rossum <guido@cwi.nl>
  16485.   ;;     Tim Peters <tim@ksr.com>
  16486. --- 1,4 ----
  16487. ! ;;; Major mode for editing Python programs, version 1.06
  16488.   ;; by: Michael A. Guravage
  16489.   ;;     Guido van Rossum <guido@cwi.nl>
  16490.   ;;     Tim Peters <tim@ksr.com>
  16491. ***************
  16492. *** 100,113 ****
  16493.       (funcall ok "/usr/tmp")
  16494.       (funcall ok "/tmp")
  16495.       (funcall ok  ".")
  16496. !     (error "Couldn't find a usable temp directory")))
  16497.     "*Directory used for temp files created by a *Python* process.
  16498.   By default, the first directory from this list that exists and that you
  16499.   can write into:  the value (if any) of the environment variable TMPDIR,
  16500.   /usr/tmp, /tmp, or the current directory.")
  16501.   
  16502. ! (defvar py-active-file nil)
  16503. ! (defvar py-pending-files nil)
  16504.   
  16505.   ;; arrange to kill temp files no matter what
  16506.   ;; have to trust that other people are as respectful of our hook
  16507. --- 100,116 ----
  16508.       (funcall ok "/usr/tmp")
  16509.       (funcall ok "/tmp")
  16510.       (funcall ok  ".")
  16511. !     (error
  16512. !      "Couldn't find a usable temp directory -- set py-temp-directory")))
  16513.     "*Directory used for temp files created by a *Python* process.
  16514.   By default, the first directory from this list that exists and that you
  16515.   can write into:  the value (if any) of the environment variable TMPDIR,
  16516.   /usr/tmp, /tmp, or the current directory.")
  16517.   
  16518. ! ;; have to bind py-file-queue before installing the kill-emacs hook
  16519. ! (defvar py-file-queue nil
  16520. !   "Queue of Python temp files awaiting execution.
  16521. ! Currently-active file is at the head of the list.")
  16522.   
  16523.   ;; arrange to kill temp files no matter what
  16524.   ;; have to trust that other people are as respectful of our hook
  16525. ***************
  16526. *** 293,307 ****
  16527.   your output from Python's output, and assumes that `>>> ' at the start
  16528.   of a line is a prompt from Python.  Similarly, the Emacs Shell mode code
  16529.   assumes that both `>>> ' and `... ' at the start of a line are Python
  16530. ! prompts.  Bad things can happen if you fool either mode."
  16531.     (interactive)
  16532.     (require 'shell)
  16533.     (switch-to-buffer-other-window (make-shell "Python" py-python-command))
  16534.     (make-local-variable 'shell-prompt-pattern)
  16535.     (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
  16536. !   (set-process-filter
  16537. !    (get-buffer-process (current-buffer))
  16538. !    'py-process-filter))
  16539.   
  16540.   (defun py-execute-region (start end)
  16541.     "Send the region between START and END to a Python interpreter.
  16542. --- 296,318 ----
  16543.   your output from Python's output, and assumes that `>>> ' at the start
  16544.   of a line is a prompt from Python.  Similarly, the Emacs Shell mode code
  16545.   assumes that both `>>> ' and `... ' at the start of a line are Python
  16546. ! prompts.  Bad things can happen if you fool either mode.
  16547. ! Warning:  If you do any editing *in* the process buffer *while* the
  16548. ! buffer is accepting output from Python, do NOT attempt to `undo' the
  16549. ! changes.  Some of the output (nowhere near the parts you changed!) may
  16550. ! be lost if you do.  This appears to be an Emacs bug, an unfortunate
  16551. ! interaction between undo and process filters; the same problem exists in
  16552. ! non-Python process buffers using the default (Emacs-supplied) process
  16553. ! filter."
  16554.     (interactive)
  16555.     (require 'shell)
  16556.     (switch-to-buffer-other-window (make-shell "Python" py-python-command))
  16557.     (make-local-variable 'shell-prompt-pattern)
  16558.     (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
  16559. !   (set-process-filter (get-buffer-process (current-buffer))
  16560. !               'py-process-filter)
  16561. !   (set-syntax-table py-mode-syntax-table))
  16562.   
  16563.   (defun py-execute-region (start end)
  16564.     "Send the region between START and END to a Python interpreter.
  16565. ***************
  16566. *** 331,358 ****
  16567.   limit is (indirectly) inherited from libc's mktemp(3).  Python mode does
  16568.   not try to protect you from exceeding the limit.  It's extremely
  16569.   unlikely that you'll get anywhere close to the limit in practice, unless
  16570. ! you're trying to be a jerk <grin>."
  16571.     (interactive "r")
  16572.     (or (< start end) (error "Region is empty"))
  16573.     (let ( (pyproc (get-process "Python"))
  16574. !      fname)
  16575. !       (if (null pyproc)
  16576. !       (shell-command-on-region start end py-python-command)
  16577. !     ;; else feed it thru a temp file
  16578. !     (setq fname (py-make-temp-name))
  16579. !     (write-region start end fname nil 'no-msg)
  16580. !     (if py-active-file
  16581. !         (progn
  16582. !           (setq py-pending-files
  16583. !             (append py-pending-files (list fname)))
  16584. !           (message "File %s queued for execution" fname))
  16585. !       ;; else
  16586. !       (py-execute-file pyproc fname)))))
  16587.   
  16588.   (defun py-execute-file (pyproc fname)
  16589. -   (if py-active-file
  16590. -       (error "Internal error in py-execute-file"))
  16591. -   (setq py-active-file fname)
  16592.     (py-append-to-process-buffer
  16593.      pyproc
  16594.      (format "## working on region in file %s ...\n" fname))
  16595. --- 342,366 ----
  16596.   limit is (indirectly) inherited from libc's mktemp(3).  Python mode does
  16597.   not try to protect you from exceeding the limit.  It's extremely
  16598.   unlikely that you'll get anywhere close to the limit in practice, unless
  16599. ! you're trying to be a jerk <grin>.
  16600. ! See the `\\[py-shell]' docs for additional warnings."
  16601.     (interactive "r")
  16602.     (or (< start end) (error "Region is empty"))
  16603.     (let ( (pyproc (get-process "Python"))
  16604. !       fname)
  16605. !     (if (null pyproc)
  16606. !     (shell-command-on-region start end py-python-command)
  16607. !       ;; else feed it thru a temp file
  16608. !       (setq fname (py-make-temp-name))
  16609. !       (write-region start end fname nil 'no-msg)
  16610. !       (setq py-file-queue (append py-file-queue (list fname)))
  16611. !       (if (cdr py-file-queue)
  16612. !       (message "File %s queued for execution" fname)
  16613. !     ;; else
  16614. !     (py-execute-file pyproc fname)))))
  16615.   
  16616.   (defun py-execute-file (pyproc fname)
  16617.     (py-append-to-process-buffer
  16618.      pyproc
  16619.      (format "## working on region in file %s ...\n" fname))
  16620. ***************
  16621. *** 390,396 ****
  16622.         (insert string)
  16623.         (move-marker pmark (point))
  16624.         (setq file-finished
  16625. !         (and py-active-file
  16626.            (equal ">>> "
  16627.               (buffer-substring
  16628.                (prog2 (beginning-of-line) (point)
  16629. --- 398,404 ----
  16630.         (insert string)
  16631.         (move-marker pmark (point))
  16632.         (setq file-finished
  16633. !         (and py-file-queue
  16634.            (equal ">>> "
  16635.               (buffer-substring
  16636.                (prog2 (beginning-of-line) (point)
  16637. ***************
  16638. *** 405,416 ****
  16639.       (set-buffer curbuf)
  16640.       (if file-finished
  16641.       (progn
  16642. !       (py-delete-files-silently py-active-file)
  16643. !       (setq py-active-file nil)
  16644. !       (if py-pending-files
  16645. !           (progn
  16646. !         (py-execute-file pyproc (car py-pending-files))
  16647. !         (setq py-pending-files (cdr py-pending-files))))))))
  16648.   
  16649.   (defun py-execute-buffer ()
  16650.     "Send the contents of the buffer to a Python interpreter.
  16651. --- 413,422 ----
  16652.       (set-buffer curbuf)
  16653.       (if file-finished
  16654.       (progn
  16655. !       (py-delete-file-silently (car py-file-queue))
  16656. !       (setq py-file-queue (cdr py-file-queue))
  16657. !       (if py-file-queue
  16658. !         (py-execute-file pyproc (car py-file-queue)))))))
  16659.   
  16660.   (defun py-execute-buffer ()
  16661.     "Send the contents of the buffer to a Python interpreter.
  16662. ***************
  16663. *** 839,849 ****
  16664.   \tif elif else try except finally for while def class
  16665.      the region will be set to the body of the structure, including
  16666.      following blocks that `belong' to it, but excluding trailing blank
  16667. !    and comment lines.  E.g., if on a `try' statement, the `try' block and
  16668. !    all (if any) of the following `except' and `finally' blocks that
  16669.      belong to the `try' structure will be in the region.  Ditto for
  16670. !    if/elif/else and for/else structures, and (a bit degenerate, since
  16671. !    they're always one-block structures) while, def and class blocks.
  16672.   
  16673.    - Else if no prefix argument is given, and the line begins a Python
  16674.      block (see list above), and the block is not a `one-liner' (i.e., the
  16675. --- 845,856 ----
  16676.   \tif elif else try except finally for while def class
  16677.      the region will be set to the body of the structure, including
  16678.      following blocks that `belong' to it, but excluding trailing blank
  16679. !    and comment lines.  E.g., if on a `try' statement, the `try' block
  16680. !    and all (if any) of the following `except' and `finally' blocks that
  16681.      belong to the `try' structure will be in the region.  Ditto for
  16682. !    if/elif/else, for/else and while/else structures, and (a bit
  16683. !    degenerate, since they're always one-block structures) def and class
  16684. !    blocks.
  16685.   
  16686.    - Else if no prefix argument is given, and the line begins a Python
  16687.      block (see list above), and the block is not a `one-liner' (i.e., the
  16688. ***************
  16689. *** 883,890 ****
  16690.        (followers
  16691.         '( (if elif else) (elif elif else) (else)
  16692.            (try except finally) (except except finally) (finally)
  16693. !          (for else)
  16694. !          (def) (class) (while) ) )
  16695.        first-symbol next-symbol)
  16696.   
  16697.       (cond
  16698. --- 890,897 ----
  16699.        (followers
  16700.         '( (if elif else) (elif elif else) (else)
  16701.            (try except finally) (except except finally) (finally)
  16702. !          (for else) (while else)
  16703. !          (def) (class) ) )
  16704.        first-symbol next-symbol)
  16705.   
  16706.       (cond
  16707. ***************
  16708. *** 1057,1070 ****
  16709.   %c:py-execute-region
  16710.   %c:py-shell
  16711.   
  16712. - Warning:  If you do any editing *in* the process buffer *while* the
  16713. - buffer is accepting output from Python, do NOT attempt to `undo' the
  16714. - changes.  Some of the output (nowhere near the parts you changed!) may
  16715. - be lost if you do.  This appears to be an Emacs bug, an unfortunate
  16716. - interaction between undo and process filters; the same problem exists in
  16717. - non-Python process buffers using the default (Emacs-supplied) process
  16718. - filter.
  16719.   @VARIABLES
  16720.   
  16721.   py-indent-offset\tindentation increment
  16722. --- 1064,1069 ----
  16723. ***************
  16724. *** 1397,1418 ****
  16725.     (make-temp-name
  16726.      (concat (file-name-as-directory py-temp-directory) "python")))
  16727.   
  16728. ! ;; pass a mixture of strings and lists of strings
  16729. ! (defun py-delete-files-silently (&rest args)
  16730. !   (let (onearg)
  16731. !     (while args
  16732. !       (setq onearg (car args)
  16733. !         args (cdr args))
  16734. !       (if (stringp onearg) (setq onearg (list onearg)))
  16735. !       (while onearg
  16736. !     (condition-case nil
  16737. !         (delete-file (car onearg))
  16738. !       (error nil))
  16739. !     (setq onearg (cdr onearg))))))
  16740.   
  16741.   (defun py-kill-emacs-hook ()
  16742.     ;; delete our temp files
  16743. !   (py-delete-files-silently py-active-file py-pending-files)
  16744.     ;; run the hook we inherited, if any
  16745.     (and py-inherited-kill-emacs-hook
  16746.          (funcall py-inherited-kill-emacs-hook)))
  16747. --- 1396,1411 ----
  16748.     (make-temp-name
  16749.      (concat (file-name-as-directory py-temp-directory) "python")))
  16750.   
  16751. ! (defun py-delete-file-silently (fname)
  16752. !   (condition-case nil
  16753. !       (delete-file fname)
  16754. !     (error nil)))
  16755.   
  16756.   (defun py-kill-emacs-hook ()
  16757.     ;; delete our temp files
  16758. !   (while py-file-queue
  16759. !     (py-delete-file-silently (car py-file-queue))
  16760. !     (setq py-file-queue (cdr py-file-queue)))
  16761.     ;; run the hook we inherited, if any
  16762.     (and py-inherited-kill-emacs-hook
  16763.          (funcall py-inherited-kill-emacs-hook)))
  16764.  
  16765. >>> END OF MSG
  16766. 
  16767. 
  16768. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  16769.     id AA26529 (5.65b/2.9/CWI-Amsterdam); Tue, 7 Apr 1992 05:51:51 +0200
  16770. Received: from uvacs.cs.virginia.edu by uvaarpa.Virginia.EDU id aa07319;
  16771.           6 Apr 92 23:51 EDT
  16772. Received: from wilbury.cs.Virginia.EDU by uvacs.cs.Virginia.EDU (4.1/5.1.UVA)
  16773.     id AA14348; Mon, 6 Apr 92 23:51:21 EDT
  16774. Posted-Date: Mon, 6 Apr 92 23:51:21 EDT
  16775. Return-Path: <mjc4y@wilbury.cs.Virginia.EDU>
  16776. Received: by wilbury.cs.Virginia.EDU (4.1/SMI-2.0)
  16777.     id AA01176; Mon, 6 Apr 92 23:51:21 EDT
  16778. Date: Mon, 6 Apr 92 23:51:21 EDT
  16779. From: mjc4y@wilbury.cs.Virginia.EDU
  16780. Message-Id: <9204070351.AA01176@wilbury.cs.Virginia.EDU>
  16781. To: python-list-request@cwi.nl
  16782. Subject: unsubscribe
  16783.  
  16784.  
  16785.  
  16786. Thanks, but someone else at my sight is already on this mailing list.
  16787. Best to give the bandwidth to someone else. :)
  16788.  
  16789.                     Matt
  16790.  
  16791. 
  16792. 
  16793. Replied: Wed, 08 Apr 1992 09:55:19 +0200
  16794. Replied: "etxmsll@juno.ericsson.se (Mats Lidell TM/JUF 91753) python-list@cwi.nl"
  16795. Received: from mailgate.ericsson.se by charon.cwi.nl with SMTP
  16796.     id AA21045 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Apr 1992 00:05:13 +0200
  16797. Received: from juno (juno.ericsson.se) by mailgate.ericsson.se (4.1/SMI-4.1-MAILGATE1.10)
  16798.     id AA12367; Wed, 8 Apr 92 00:05:09 +0200
  16799. Received: by juno (4.1/SMI-4.1-LME1.6)
  16800.     id AA10855; Wed, 8 Apr 92 00:05:06 +0200
  16801. Date: Wed, 8 Apr 92 00:05:06 +0200
  16802. From: etxmsll@juno.ericsson.se (Mats Lidell TM/JUF 91753)
  16803. Message-Id: <9204072205.AA10855@juno>
  16804. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  16805. Cc: python-list@cwi.nl
  16806. Subject: Python release 0.9.6 is here!
  16807. In-Reply-To: <9204061421.AA20700@voorn.cwi.nl>
  16808. References: <9204061421.AA20700@voorn.cwi.nl>
  16809.  
  16810. >>>>> "Guido" == Guido van Rossum <Guido.van.Rossum@cwi.nl> writes:
  16811.  
  16812.   Guido> You are encouraged to port this to your system and report any
  16813.   Guido> problems you have.
  16814.  
  16815. OOPS! This happens to me. (I'm on a sun4 using sun vanilla cc. No
  16816. extra features selected for python.)
  16817.  
  16818. % dbx python
  16819. Reading symbolic information...
  16820. Read 17176 symbols
  16821. (dbx) run
  16822. Running: python 
  16823. Python 0.9.6 (>= 1 Jan 1992).
  16824. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  16825. >>> import testall
  16826. test_grammar
  16827. attempt to read stack failed - bad frame pointer
  16828. (dbx) where
  16829. _doprnt() at 0xf76e4ff4
  16830. attempt to read stack failed - bad frame pointer
  16831.  
  16832. %% Mats
  16833. 
  16834. 
  16835. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  16836.     id AA12634 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Apr 1992 09:55:21 +0200
  16837. Received: by voorn.cwi.nl with SMTP; Wed, 8 Apr 1992 07:55:21 GMT
  16838. Message-Id: <9204080755.AA05556@voorn.cwi.nl>
  16839. To: etxmsll@juno.ericsson.se (Mats Lidell TM/JUF 91753)
  16840. Cc: python-list@cwi.nl
  16841. Subject: Re: Python release 0.9.6 is here! 
  16842. In-Reply-To: Your message of "Wed, 08 Apr 1992 00:05:06 MDT."
  16843.              <9204072205.AA10855@juno> 
  16844. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  16845. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  16846. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  16847. Date: Wed, 08 Apr 1992 09:55:20 +0200
  16848. Sender: Guido.van.Rossum@cwi.nl
  16849.  
  16850. >OOPS! This happens to me. (I'm on a sun4 using sun vanilla cc. No
  16851. >extra features selected for python.)
  16852. >[dbx session transcript deleted]
  16853.  
  16854. Could it be that you have set your stacksize limit too low?  By doing
  16855. "limit stacksize 10" I can force Python to dump core, for instance.
  16856. A limit of 100 seems to be sufficient to run the tests, at least.
  16857.  
  16858. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  16859. "That was never five minutes just now!"
  16860. "I'm afraid it was."
  16861. 
  16862. 
  16863. Replied: Thu, 09 Apr 1992 10:02:17 +0200
  16864. Replied: "dussud%aspen@lucid.com (Patrick Dussud) python-list@cwi.nl"
  16865. Received: from lucid.com by charon.cwi.nl with SMTP
  16866.     id AA24312 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Apr 1992 17:55:46 +0200
  16867. Received: from aspen.lucid (aspen.lucid.com) by heavens-gate.lucid.com id AA25249g; Wed, 8 Apr 92 08:49:19 PDT
  16868. Received: by aspen.lucid (4.1/SMI-4.1)
  16869.     id AA12736; Wed, 8 Apr 92 08:55:35 PDT
  16870. Date: Wed, 8 Apr 92 08:55:35 PDT
  16871. From: dussud%aspen@lucid.com (Patrick Dussud)
  16872. Message-Id: <9204081555.AA12736@aspen.lucid>
  16873. To: python-list@cwi.nl
  16874. Subject: Problem with (KeyboardInterrupt and wdb
  16875.  
  16876.  
  16877. Here is a problem that I found:
  16878. python dumps core if I use wdb and type ^C:
  16879.  
  16880. aspen% python
  16881. Python 0.9.6 (>= 1 Jan 1992).
  16882. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  16883. >>> import sys
  16884. >>> sys.path.append ('../demo/scripts')
  16885. >>> import wdb
  16886. >>> wdb.run ('import pi')
  16887. 31415926535897932384626433832Segmentation fault (core dumped)
  16888.  
  16889.                              ^C right here
  16890.  
  16891.  
  16892. Below is what I found out under gdb:
  16893.  
  16894. Program received signal 11, Segmentation fault
  16895. Reading symbols from ceval.c...done.
  16896. eval_code (co=(codeobject *) 0xa61f0, globals=(struct _object *) 0xa3df0, locals=(struct _object *) 0xe3aa0, arg=(struct _object *) 0xe3a60) (ceval.c:778)
  16897. (gdb) p w
  16898. $1 = (struct _object *) 0x0
  16899. (gdb) info loc
  16900. ticker = 94
  16901. next_instr = (unsigned char *) 0xa5cab "Z\002"
  16902. opcode = 92
  16903. oparg = 1
  16904. stack_pointer = (struct _object **) 0xe4884
  16905. why = WHY_NOT
  16906. err = 0
  16907. x = (struct _object *) 0x7f528
  16908. v = (struct _object *) 0xe3d80
  16909. w = (struct _object *) 0x0
  16910. u = (struct _object *) 0x0
  16911. t = (struct _object *) 0x7f528
  16912. f = (struct _frame *) 0xe4820
  16913. trace = (struct _object *) 0x0
  16914. retval = (struct _object *) 0xa3248
  16915. name = (char *) 0xc28cc "stopframe"
  16916. fp = (struct _iobuf *) 0x1a
  16917. (gdb) bt
  16918. #0  eval_code (co=(codeobject *) 0xa61f0, globals=(struct _object *) 0xa3df0, locals=(struct _object *) 0xe3aa0, arg=(struct _object *) 0xe3a60) (ceval.c:778)
  16919. #1  call_function (func=(struct _object *) 0xcd4e0, arg=(struct _object *) 0xe3a60) (ceval.c:1689)
  16920. #2  call_object (func=(struct _object *) 0xe3980, arg=(struct _object *) 0xe3a20) (ceval.c:1589)
  16921. #3  eval_code (co=(codeobject *) 0xc2680, globals=(struct _object *) 0xc1060, locals=(struct _object *) 0xe43e0, arg=(struct _object *) 0xe43a0) (ceval.c:385)
  16922. #4  call_function (func=(struct _object *) 0xc9e40, arg=(struct _object *) 0xe43a0) (ceval.c:1689)
  16923. #5  call_object (func=(struct _object *) 0xe3be0, arg=(struct _object *) 0xe3ec0) (ceval.c:1589)
  16924. #6  eval_code (co=(codeobject *) 0xbca60, globals=(struct _object *) 0xc1060, locals=(struct _object *) 0xe3e80, arg=(struct _object *) 0xe3e40) (ceval.c:385)
  16925. #7  call_function (func=(struct _object *) 0xc9e20, arg=(struct _object *) 0xe3e40) (ceval.c:1689)
  16926. #8  call_object (func=(struct _object *) 0xe3f60, arg=(struct _object *) 0xe3dc0) (ceval.c:1589)
  16927. #9  call_trace (p_trace=(struct _object **) 0xf7fff024, p_newtrace=(struct _object **) 0xf7fff024, f=(struct _frame *) 0xd6260, msg=(char *) 0x7acb4 "exception", arg=(struct _object *) 0xe3d80) (ceval.c:1247)
  16928. #10 call_exc_trace (p_trace=(struct _object **) 0xf7fff024, p_newtrace=(struct _object **) 0xf7fff024, f=(struct _frame *) 0xd6260) (ceval.c:1199)
  16929. #11 eval_code (co=(codeobject *) 0xd4ac0, globals=(struct _object *) 0x9a770, locals=(struct _object *) 0x9a770, arg=(struct _object *) 0x0) (ceval.c:1066)
  16930. #12 eval_node ()
  16931. #13 run_err_node ()
  16932. #14 run_string ()
  16933. #15 exec_eval ()
  16934. #16 builtin_exec ()
  16935. #17 call_builtin (func=(struct _object *) 0x98ff0, arg=(struct _object *) 0xd45c0) (ceval.c:1609)
  16936. #18 call_object (func=(struct _object *) 0x98ff0, arg=(struct _object *) 0xd45c0) (ceval.c:1591)
  16937. #19 eval_code (co=(codeobject *) 0xc7560, globals=(struct _object *) 0xc1060, locals=(struct _object *) 0xd5720, arg=(struct _object *) 0xd56e0) (ceval.c:385)
  16938. #20 call_function (func=(struct _object *) 0xca020, arg=(struct _object *) 0xd56e0) (ceval.c:1689)
  16939. #21 call_object (func=(struct _object *) 0xd5540, arg=(struct _object *) 0xd5640) (ceval.c:1589)
  16940. #22 eval_code (co=(codeobject *) 0xc7040, globals=(struct _object *) 0xc1060, locals=(struct _object *) 0xd5600, arg=(struct _object *) 0xd55c0) (ceval.c:385)
  16941. #23 call_function (func=(struct _object *) 0xca000, arg=(struct _object *) 0xd55c0) (ceval.c:1689)
  16942. #24 call_object (func=(struct _object *) 0xd4e40, arg=(struct _object *) 0xd4e60) (ceval.c:1589)
  16943. #25 eval_code (co=(codeobject *) 0xac050, globals=(struct _object *) 0xa3df0, locals=(struct _object *) 0xd5260, arg=(struct _object *) 0xd5220) (ceval.c:385)
  16944. #26 call_function (func=(struct _object *) 0xcaa80, arg=(struct _object *) 0xd5220) (ceval.c:1689)
  16945. #27 call_object (func=(struct _object *) 0xcaa80, arg=(struct _object *) 0xd5220) (ceval.c:1589)
  16946. #28 eval_code (co=(codeobject *) 0xd4800, globals=(struct _object *) 0x9a770, locals=(struct _object *) 0x9a770, arg=(struct _object *) 0x0) (ceval.c:385)
  16947. #29 eval_node ()
  16948. #30 run_tty_1 ()
  16949. #31 run_tty_loop ()
  16950. #32 main ()
  16951. (gdb) p *(tupleobject*)v
  16952. $6 = {ob_refcnt = 10, ob_type = 0x804f8, ob_size = 3, ob_item = {0x99430}}
  16953.  
  16954. (gdb) p ((tupleobject*)v)->ob_item[0]
  16955. $8 = (struct _object *) 0x99430
  16956. (gdb) p ((tupleobject*)v)->ob_item[1]
  16957. $9 = (struct _object *) 0x0
  16958.  
  16959. ob_item [1] is 0 so w becomes 0
  16960.  
  16961. (gdb) p ((tupleobject*)v)->ob_item[2]
  16962. $10 = (struct _object *) 0xe3d40
  16963.  
  16964. 
  16965. 
  16966. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  16967.     id AA24739 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Apr 1992 18:08:09 +0200
  16968. Received: from uvacs.cs.virginia.edu by uvaarpa.Virginia.EDU id aa29683;
  16969.           8 Apr 92 12:07 EDT
  16970. Received: from ash.cs.Virginia.EDU by uvacs.cs.Virginia.EDU (4.1/5.1.UVA)
  16971.     id AA24206; Wed, 8 Apr 92 12:07:38 EDT
  16972. Posted-Date: Wed, 8 Apr 92 12:07:37 EDT
  16973. Return-Path: <spm2d@ash.cs.Virginia.EDU>
  16974. Received: by ash.cs.Virginia.EDU (4.1/SMI-2.0)
  16975.     id AA04932; Wed, 8 Apr 92 12:07:37 EDT
  16976. Date: Wed, 8 Apr 92 12:07:37 EDT
  16977. From: spm2d@ash.cs.Virginia.EDU
  16978. Message-Id: <9204081607.AA04932@ash.cs.Virginia.EDU>
  16979. To: python-list@cwi.nl
  16980. Subject: Python 0.9.6 and SPARCs
  16981.  
  16982. I'm having problems getting the new Python to work on
  16983. the SPARCstations here. Specifically, when I do a floating
  16984. point multiplication or divide Python core dumps. The dbx trace
  16985. doesn't help, as I just get garbage, usually with some mention of a
  16986. shared library.
  16987.  
  16988. It works fine on the SGIs.
  16989.  
  16990. I've tried using both cc and gcc to no avail.
  16991.  
  16992. Is there something I have to do to the Makefile or strtod.c?
  16993. (and if yours works, could you please send me your version?)
  16994.  
  16995. Thanks,
  16996. Steve
  16997. 
  16998. 
  16999. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  17000.     id AA25393 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Apr 1992 18:26:42 +0200
  17001. Received: from uvacs.cs.virginia.edu by uvaarpa.Virginia.EDU id aa01005;
  17002.           8 Apr 92 12:25 EDT
  17003. Received: from ash.cs.Virginia.EDU by uvacs.cs.Virginia.EDU (4.1/5.1.UVA)
  17004.     id AA25370; Wed, 8 Apr 92 12:25:57 EDT
  17005. Posted-Date: Wed, 8 Apr 92 12:25:56 EDT
  17006. Return-Path: <spm2d@ash.cs.Virginia.EDU>
  17007. Received: by ash.cs.Virginia.EDU (4.1/SMI-2.0)
  17008.     id AA05254; Wed, 8 Apr 92 12:25:56 EDT
  17009. Date: Wed, 8 Apr 92 12:25:56 EDT
  17010. From: spm2d@ash.cs.Virginia.EDU
  17011. Message-Id: <9204081625.AA05254@ash.cs.Virginia.EDU>
  17012. To: python-list@cwi.nl
  17013. Subject: SPARC problem fixed
  17014.  
  17015. OK, I solved the problem by cleverly reading lines in the makefile
  17016. that mention strtod isn't needed under SunOS 4. In fact, if you
  17017. include them, Python crashes.
  17018.  
  17019. Steve
  17020. 
  17021. 
  17022. Received: from lucid.com by charon.cwi.nl with SMTP
  17023.     id AA01577 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Apr 1992 23:10:42 +0200
  17024. Received: from aspen.lucid (aspen.lucid.com) by heavens-gate.lucid.com id AA26295g; Wed, 8 Apr 92 14:04:24 PDT
  17025. Received: by aspen.lucid (4.1/SMI-4.1)
  17026.     id AA13134; Wed, 8 Apr 92 14:10:40 PDT
  17027. Date: Wed, 8 Apr 92 14:10:40 PDT
  17028. From: dussud%aspen@lucid.com (Patrick Dussud)
  17029. Message-Id: <9204082110.AA13134@aspen.lucid>
  17030. To: python-list@cwi.nl
  17031. Subject: small bug in posixpath.py
  17032.  
  17033.  
  17034. def samestat(s1, s2):
  17035.     return s1[stat.ST_INO] == s2[stat.ST_INO] and \
  17036.         s1[stat.ST_DEV] == s2[stat.ST_DEV]
  17037.                                            ^^ not STD_DEV
  17038. 
  17039. 
  17040. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17041.     id AA18497 (5.65b/2.10/CWI-Amsterdam); Thu, 9 Apr 1992 10:02:36 +0200
  17042. Received: by voorn.cwi.nl with SMTP; Thu, 9 Apr 1992 08:02:21 GMT
  17043. Message-Id: <9204090802.AA09972@voorn.cwi.nl>
  17044. To: dussud%aspen@lucid.com (Patrick Dussud)
  17045. Cc: python-list@cwi.nl
  17046. Subject: Re: Problem with (KeyboardInterrupt and wdb 
  17047. In-Reply-To: Your message of "Wed, 08 Apr 1992 08:55:35 MDT."
  17048.              <9204081555.AA12736@aspen.lucid> 
  17049. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17050. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17051. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17052. Date: Thu, 09 Apr 1992 10:02:20 +0200
  17053. Sender: Guido.van.Rossum@cwi.nl
  17054.  
  17055. Patrick Dussud reported a Python core dump when a program being traced
  17056. by wdb or pdb is interrupted.  In private e-mail he suggested a fix.
  17057. Here's my version of that fix.  Note that this isn't an official patch
  17058. to Python, just a little band-aid if you're worried about it...
  17059.  
  17060. *** /tmp/,RCSt1a09915    Thu Apr  9 09:47:21 1992
  17061. --- ceval.c    Thu Apr  9 09:45:03 1992
  17062. ***************
  17063. *** 1189,1194 ****
  17064. --- 1189,1198 ----
  17065.       object *type, *value, *traceback, *arg;
  17066.       int err;
  17067.       err_get(&type, &value);
  17068. +     if (value == NULL) {
  17069. +         value = None;
  17070. +         INCREF(value);
  17071. +     }
  17072.       traceback = tb_fetch();
  17073.       arg = newtupleobject(3);
  17074.       if (arg == NULL)
  17075.  
  17076. Hats off to Patrick for finding and solving this so quickly!
  17077.  
  17078. BTW, just in case anyone missed it, it is now official that on a Sparc
  17079.      DON'T USE THE STRTOD.C SOURCE DISTRIBUTED WITH PYTHON!
  17080.  
  17081. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17082. "I've got a pet halibut called Eric.  I chose him out of thousands,
  17083. the others were all to flat."
  17084. 
  17085. 
  17086. Received: from albert.gnu.ai.mit.edu by charon.cwi.nl with SMTP
  17087.     id AA20348 (5.65b/2.10/CWI-Amsterdam); Sat, 11 Apr 1992 08:45:27 +0200
  17088. Received: from hal.gnu.ai.mit.edu by albert.gnu.ai.mit.edu (5.65/4.0) with SMTP
  17089.     id <AA27691@albert.gnu.ai.mit.edu>; Sat, 11 Apr 92 02:45:22 -0400
  17090. Received: by hal.gnu.ai.mit.edu (AIX 3.2/UCB 5.64/4.0)
  17091.     id <AA20876@hal.gnu.ai.mit.edu>; Sat, 11 Apr 1992 02:45:20 -0400
  17092. From: cstruble@gnu.ai.mit.edu
  17093. Message-Id: <9204110645.AA20876@hal.gnu.ai.mit.edu>
  17094. Subject: Request for list
  17095. To: python-list-request@cwi.nl
  17096. Date: Sat, 11 Apr 92 2:45:20 EDT
  17097.  
  17098. This is my request for the python list.
  17099. Here's the info you requested
  17100.  
  17101. Name: Craig Struble
  17102. E-mail: cstruble@gnu.ai.mit.edu
  17103.  
  17104.     Thanks,
  17105.         Craig
  17106. -- 
  17107.      OOP : When people object to your orientation towards programming...
  17108. Craig Struble                            | cstruble@gnu.ai.mit.edu
  17109. Consciousness increases development time | cstruble@csugrad.cs.vt.edu
  17110. A/UX Hacker Wannabe :-o                  | cstruble@toaster.async.vt.edu (SLIP)
  17111. 
  17112. 
  17113. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17114.     id AA04716 (5.65b/2.10/CWI-Amsterdam); Thu, 16 Apr 1992 11:05:30 +0200
  17115. Received: by voorn.cwi.nl with SMTP; Thu, 16 Apr 1992 09:05:29 GMT
  17116. Message-Id: <9204160905.AA26961@voorn.cwi.nl>
  17117. To: python-list@cwi.nl
  17118. Subject: comments please!
  17119. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17120. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17121. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17122. Date: Thu, 16 Apr 1992 11:05:27 +0200
  17123. Sender: Guido.van.Rossum@cwi.nl
  17124.  
  17125. I am trying to get ready to post the complete Python sources to
  17126. comp.sources.unix (or somewhere else if the c.s.u moderators don't
  17127. accept it) so I can finally bump the version number to 1.0.  There
  17128. may be one round of patches posted to this mailing list first, to
  17129. correct minor things; this will create version 0.9.7.
  17130.  
  17131. The main think I'm currently worried about is the documentation.  If
  17132. you have comments, either very detailed or on the general structure
  17133. and quality, please let me know!  Of course, any other comments are
  17134. also welcome, but the point is that if the software doesn't work, I
  17135. get complaints immediately, while if there's a problem with the docs,
  17136. most people will figure it out anyway...
  17137.  
  17138. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17139. "Are all your pets called Eric?"
  17140. 
  17141. 
  17142. Replied: Fri, 17 Apr 1992 16:23:57 +0200
  17143. Replied: "cstruble@gnu.ai.mit.edu python-list@cwi.nl"
  17144. Received: from albert.gnu.ai.mit.edu by charon.cwi.nl with SMTP
  17145.     id AA17985 (5.65b/2.10/CWI-Amsterdam); Thu, 16 Apr 1992 20:57:31 +0200
  17146. Received: from hal.gnu.ai.mit.edu by albert.gnu.ai.mit.edu (5.65/4.0) with SMTP
  17147.     id <AA16639@albert.gnu.ai.mit.edu>; Thu, 16 Apr 92 14:57:24 -0400
  17148. Received: by hal.gnu.ai.mit.edu (AIX 3.2/UCB 5.64/4.0)
  17149.     id <AA28948@hal.gnu.ai.mit.edu>; Thu, 16 Apr 1992 14:57:21 -0400
  17150. From: cstruble@gnu.ai.mit.edu
  17151. Message-Id: <9204161857.AA28948@hal.gnu.ai.mit.edu>
  17152. Subject: Bug in 0.9.6
  17153. To: python-list@cwi.nl
  17154. Date: Thu, 16 Apr 92 14:57:20 EDT
  17155.  
  17156. The only thing I've noticed so far is the use of the finally: clause. The
  17157. documentation states that a finally: clause needs to be placed after all
  17158. except statements, but python 0.9.6 won't accept a finally: clause at all if
  17159. except statements exist. For example, you can have:
  17160.     try:
  17161.         code...
  17162.     except:
  17163.         more code...
  17164.  
  17165. or
  17166.     try:
  17167.         code...
  17168.     finally:
  17169.         more code...
  17170.  
  17171. but not
  17172.     try:
  17173.         code...
  17174.     except:
  17175.         more code...
  17176.     finally:
  17177.         even more code...
  17178.  
  17179. This bug, or misfeature showed up when trying to run the python.py script.
  17180. Since I've just joined this group, I'm not sure if this bug has already
  17181. been reported. If this is a repeat bug, sorry.
  17182.     See ya later,
  17183.         Craig
  17184. -- 
  17185.                                 Huh?
  17186. Craig Struble                            | cstruble@gnu.ai.mit.edu
  17187. Consciousness increases development time | cstruble@csugrad.cs.vt.edu
  17188. A/UX Hacker Wannabe :-o                  | cstruble@toaster.async.vt.edu (SLIP)
  17189. 
  17190. 
  17191. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17192.     id AA17017 (5.65b/2.10/CWI-Amsterdam); Fri, 17 Apr 1992 16:24:01 +0200
  17193. Received: by voorn.cwi.nl with SMTP; Fri, 17 Apr 1992 14:24:00 GMT
  17194. Message-Id: <9204171424.AA00298@voorn.cwi.nl>
  17195. To: cstruble@gnu.ai.mit.edu
  17196. Cc: python-list@cwi.nl
  17197. Subject: Re: Bug in 0.9.6 
  17198. In-Reply-To: Your message of "Thu, 16 Apr 1992 14:57:20 MDT."
  17199.              <9204161857.AA28948@hal.gnu.ai.mit.edu> 
  17200. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17201. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17202. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17203. Date: Fri, 17 Apr 1992 16:24:00 +0200
  17204. Sender: Guido.van.Rossum@cwi.nl
  17205.  
  17206. >The only thing I've noticed so far is the use of the finally: clause. The
  17207. >documentation states that a finally: clause needs to be placed after all
  17208. >except statements, but python 0.9.6 won't accept a finally: clause at all if
  17209. >except statements exist.
  17210.  
  17211. This was a last-minute change to the grammar; in the previous version
  17212. a try clause could have both except clauses and a finally clause; in
  17213. the new version it can have only one of the two kinds.  The reason for
  17214. the change is that it is both easier to explain and easier to
  17215. implement this way, and that the combination is rarely needed anyway.
  17216.  
  17217. I must admit that I haven't tried the python.py script for a long time
  17218. (since the Emacs python-mode.el has similar functionality); I'll fix
  17219. it for the next release.
  17220.  
  17221. Where in the docs did you see the old situation described?  I'm pretty
  17222. sure the reference manual describes the new version.
  17223.  
  17224. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17225. "It has ceased to be"
  17226. 
  17227. 
  17228. Received: from lucid.com by charon.cwi.nl with SMTP
  17229.     id AA02895 (5.65b/2.10/CWI-Amsterdam); Tue, 12 May 1992 18:07:21 +0200
  17230. Received: from aspen.lucid (aspen.lucid.com) by heavens-gate.lucid.com id AA14008g; Tue, 12 May 92 08:59:46 PDT
  17231. Received: by aspen.lucid (4.1/SMI-4.1)
  17232.     id AA03288; Tue, 12 May 92 09:05:36 PDT
  17233. Date: Tue, 12 May 92 09:05:36 PDT
  17234. From: dussud%aspen@lucid.com (Patrick Dussud)
  17235. Message-Id: <9205121605.AA03288@aspen.lucid>
  17236. To: python-list@cwi.nl
  17237. Subject: MSDOS version of python
  17238.  
  17239.  
  17240. Someone I know would like to get an executable version of python for an MSDOS
  17241. machine, but I don't think we have a decent MSDOS compiler around here. Can
  17242. someone give me a pointer to an MSDOS version of python? 
  17243. Thanks,
  17244. Patrick.
  17245. 
  17246. 
  17247. Replied: Tue, 19 May 1992 15:05:50 +0200
  17248. Replied: " python-list@cwi.nl"
  17249. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17250.     id AA25659 (5.65b/2.10/CWI-Amsterdam); Tue, 19 May 1992 09:44:56 +0200
  17251. Received: by voorn.cwi.nl with SMTP; Tue, 19 May 1992 07:44:56 GMT
  17252. Message-Id: <9205190744.AA27764@voorn.cwi.nl>
  17253. To: python-list@cwi.nl
  17254. Subject: MS-DOS Python binary available
  17255. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17256. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17257. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17258. Date: Tue, 19 May 1992 09:44:54 +0200
  17259. Sender: Guido.van.Rossum@cwi.nl
  17260.  
  17261. Thanks to Mark Anacker I can now offer a binary of Python 0.9.6
  17262. compiled for MS-DOS computers.
  17263.  
  17264. You can ftp it from ftp.cwi.nl:/pub/python.exe.Z; use (UNIX)
  17265. uncompress to turn it into a PC binary and then transfer to the PC
  17266. using your favorite file transfer method.
  17267.  
  17268. If you want to use the standard Python library, you'll also have to
  17269. transfer the contents of the "lib" subdirectory of the Python source
  17270. distribution to your PC.
  17271.  
  17272. (Note that I am totally ignorant about PC things -- if there's a
  17273. better way to set things up, please let me know!)
  17274.  
  17275. Thanks, Mark!
  17276.  
  17277. --Guido
  17278. 
  17279. 
  17280. To: python-list@cwi.nl
  17281. Subject: MS-DOS Python binary available
  17282. From: Guido van Rossum <guido@cwi.nl>
  17283. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17284. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17285. Date: Tue, 19 May 1992 09:44:54 +0200
  17286. Sender: guido
  17287.  
  17288. Thanks to Mark Anacker I can now offer a binary of Python 0.9.6
  17289. compiled for MS-DOS computers.
  17290.  
  17291. You can ftp it from ftp.cwi.nl:/pub/python.exe.Z; use (UNIX)
  17292. uncompress to turn it into a PC binary and then transfer to the PC
  17293. using your favorite file transfer method.
  17294.  
  17295. If you want to use the standard Python library, you'll also have to
  17296. transfer the contents of the "lib" subdirectory of the Python source
  17297. distribution to your PC.
  17298.  
  17299. (Note that I am totally ignorant about PC things -- if there's a
  17300. better way to set things up, please let me know!)
  17301.  
  17302. Thanks, Mark!
  17303.  
  17304. --Guido
  17305. 
  17306. 
  17307. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17308.     id AA02291 (5.65b/2.10/CWI-Amsterdam); Tue, 19 May 1992 15:05:52 +0200
  17309. Received: by voorn.cwi.nl with SMTP; Tue, 19 May 1992 13:05:51 GMT
  17310. Message-Id: <9205191305.AA01395@voorn.cwi.nl>
  17311. To: python-list@cwi.nl
  17312. Subject: Re: MS-DOS Python binary available 
  17313. In-Reply-To: Your message of "Tue, 19 May 1992 09:44:54 MDT."
  17314.              <9205190744.AA27764@voorn.cwi.nl> 
  17315. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17316. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17317. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17318. Date: Tue, 19 May 1992 15:05:50 +0200
  17319. Sender: Guido.van.Rossum@cwi.nl
  17320.  
  17321. A few hours ago I wrote:
  17322.  
  17323. >Thanks to Mark Anacker I can now offer a binary of Python 0.9.6
  17324. >compiled for MS-DOS computers.
  17325. >
  17326. >You can ftp it from ftp.cwi.nl:/pub/python.exe.Z [...]
  17327.  
  17328. I've since then located a copy of ZIP that works on UNIX.  Since ZIP
  17329. files are both smaller and more popular in the PC world, I've replaced
  17330. the python.exe.Z file by python0.9.6.zip.
  17331.  
  17332. As always, use binary ftp mode!
  17333.  
  17334. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17335. "Hello, I'm your new vicar.  Can I interest you in any encyclopaedias?"
  17336. 
  17337. 
  17338. cc: python-list@cwi.nl
  17339. Subject: Re: MS-DOS Python binary available 
  17340. In-reply-to: Your message of "Tue, 19 May 1992 09:44:54 MDT."
  17341.              <9205190744.AA27764@voorn.cwi.nl> 
  17342. From: Guido van Rossum <guido@cwi.nl>
  17343. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17344. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17345. Date: Tue, 19 May 1992 15:05:50 +0200
  17346. Sender: guido
  17347.  
  17348. A few hours ago I wrote:
  17349.  
  17350. >Thanks to Mark Anacker I can now offer a binary of Python 0.9.6
  17351. >compiled for MS-DOS computers.
  17352. >
  17353. >You can ftp it from ftp.cwi.nl:/pub/python.exe.Z [...]
  17354.  
  17355. I've since then located a copy of ZIP that works on UNIX.  Since ZIP
  17356. files are both smaller and more popular in the PC world, I've replaced
  17357. the python.exe.Z file by python0.9.6.zip.
  17358.  
  17359. As always, use binary ftp mode!
  17360.  
  17361. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17362. "Hello, I'm your new vicar.  Can I interest you in any encyclopaedias?"
  17363. 
  17364. 
  17365. Received: from lucid.com by charon.cwi.nl with SMTP
  17366.     id AA04029 (5.65b/2.10/CWI-Amsterdam); Tue, 19 May 1992 16:33:03 +0200
  17367. Received: from aspen.lucid (aspen.lucid.com) by heavens-gate.lucid.com id AA18366g; Tue, 19 May 92 07:25:23 PDT
  17368. Received: by aspen.lucid (4.1/SMI-4.1)
  17369.     id AA16827; Tue, 19 May 92 07:32:56 PDT
  17370. Date: Tue, 19 May 92 07:32:56 PDT
  17371. From: dussud%aspen@lucid.com (Patrick Dussud)
  17372. Message-Id: <9205191432.AA16827@aspen.lucid>
  17373. To: Guido.van.Rossum@cwi.nl
  17374. Cc: python-list@cwi.nl
  17375. In-Reply-To: <9205190744.AA27764@voorn.cwi.nl> (message from Guido van Rossum on Tue, 19 May 1992 09:44:54 +0200)
  17376. Subject: MS-DOS Python binary available
  17377.  
  17378.  
  17379. Thanks Mark and Guido!
  17380. Patrick.
  17381. 
  17382. 
  17383. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  17384.     id AA08803 (5.65b/2.10/CWI-Amsterdam); Tue, 19 May 1992 20:29:07 +0200
  17385. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa16948;
  17386.           19 May 92 14:28 EDT
  17387. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  17388.     id AA08215; Tue, 19 May 92 14:27:51 EDT
  17389. Date: Tue, 19 May 92 14:27:51 EDT
  17390. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  17391. Message-Id: <9205191827.AA08215@aemsun.med.Virginia.EDU>
  17392. To: python-list@cwi.nl
  17393. Subject: MS-DOS python
  17394.  
  17395.  
  17396. I started on compiling python for MS-DOS but have now 
  17397. been moved to a Macintosh project, so I haven't had a 
  17398. chance to get back to it. 
  17399.  
  17400. I *did* need to truncate the "#include filename" filenames
  17401. to get anything to compile. [ With BORLAND C ]
  17402.  
  17403. Guido, here is the awk script I used, in case you want to fold it
  17404. into the distribution. Replaces all 
  17405.  
  17406. #include longfilename.h    with:
  17407.  
  17408. #ifdef __MSDOS__
  17409. #include longfile.h
  17410. #else
  17411. #include longfilename.h
  17412. #endif
  17413.  
  17414. It doesn't try to do anything with "#include <filename>", but all of
  17415. those occurances seem to be short enough names. 
  17416. It does not look like truncation produces any duplicate names. 
  17417.  
  17418.  
  17419. /^#[ \t]*include[ \t]*"[A-Za-z0-9]*/     { \
  17420.         file = $2;
  17421.         n = split( file, a, "." );
  17422.         name = a[1];
  17423.         ext = a[2];
  17424.         if ( length( name ) < 10 )  {
  17425.           print ;
  17426.         }
  17427.         else { 
  17428.           print "#ifdef __MSDOS__ ", "\t/*** Truncate file name ***/" ;
  17429.           print $1, substr(name,0,9) "."  ext;
  17430.           print "#else";
  17431.           print; 
  17432.           print "#endif","\t/*** MSDOS *** Truncate file name ***/" ;
  17433.         }
  17434.        }
  17435. ! /^#[ \t]*include[ \t]*"[A-Za-z0-9]*/      {  print  } 
  17436.  
  17437.  
  17438.  
  17439.  
  17440. Was this step *not* needed with MSC ( or whatever was used ) ? 
  17441.  
  17442. Any other tips/problems noted ? 
  17443. [ Otherwise, I've been busy learning ( but not liking C++ ). ]
  17444.  
  17445.  
  17446. - Steve Majewski
  17447.  
  17448.  
  17449. 
  17450. 
  17451. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17452.     id AA11959 (5.65b/2.10/CWI-Amsterdam); Wed, 20 May 1992 14:39:10 +0200
  17453. Received: by voorn.cwi.nl with SMTP; Wed, 20 May 1992 12:39:09 GMT
  17454. Message-Id: <9205201239.AA04024@voorn.cwi.nl>
  17455. To: python-list@cwi.nl
  17456. Subject: Turbo C MS-DOS patches to Python
  17457. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17458. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17459. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17460. Date: Wed, 20 May 1992 14:39:09 +0200
  17461. Sender: Guido.van.Rossum@cwi.nl
  17462.  
  17463. This is just a little note telling that Mark's patches for Python
  17464. 0.9.6 to make it compile with Turbo C (Borland 2.0) under MS-DOS are
  17465. now available for anonymous ftp as
  17466.  
  17467.     ftp.cwi.nl:/pub/python-dos-patch
  17468.  
  17469. (Mark, do you have any interesting Python modules that use your pc
  17470. module?  Would you care to post them to the list as examples?)
  17471.  
  17472. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17473. "Fear, surprise, ruthless efficiency, a fanatical devotion to the
  17474. pope, and nice red uniforms."
  17475. 
  17476. 
  17477. To: python-list@cwi.nl
  17478. Subject: Turbo C MS-DOS patches to Python
  17479. From: Guido van Rossum <guido@cwi.nl>
  17480. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17481. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17482. Date: Wed, 20 May 1992 14:39:09 +0200
  17483. Sender: guido
  17484.  
  17485. This is just a little note telling that Mark's patches for Python
  17486. 0.9.6 to make it compile with Turbo C (Borland 2.0) under MS-DOS are
  17487. now available for anonymous ftp as
  17488.  
  17489.     ftp.cwi.nl:/pub/python-dos-patch
  17490.  
  17491. (Mark, do you have any interesting Python modules that use your pc
  17492. module?  Would you care to post them to the list as examples?)
  17493.  
  17494. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17495. "Fear, surprise, ruthless efficiency, a fanatical devotion to the
  17496. pope, and nice red uniforms."
  17497. 
  17498. 
  17499. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17500.     id AA11959 (5.65b/2.10/CWI-Amsterdam); Wed, 20 May 1992 14:39:10 +0200
  17501. Received: by voorn.cwi.nl with SMTP; Wed, 20 May 1992 12:39:09 GMT
  17502. Message-Id: <9205201239.AA04024@voorn.cwi.nl>
  17503. To: python-list@cwi.nl
  17504. Subject: Turbo C MS-DOS patches to Python
  17505. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17506. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17507. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17508. Date: Wed, 20 May 1992 14:39:09 +0200
  17509. Sender: Guido.van.Rossum@cwi.nl
  17510.  
  17511. This is just a little note telling that Mark's patches for Python
  17512. 0.9.6 to make it compile with Turbo C (Borland 2.0) under MS-DOS are
  17513. now available for anonymous ftp as
  17514.  
  17515.     ftp.cwi.nl:/pub/python-dos-patch
  17516.  
  17517. (Mark, do you have any interesting Python modules that use your pc
  17518. module?  Would you care to post them to the list as examples?)
  17519.  
  17520. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17521. "Fear, surprise, ruthless efficiency, a fanatical devotion to the
  17522. pope, and nice red uniforms."
  17523. 
  17524. 
  17525. Received: from mcsun.EU.net by charon.cwi.nl with SMTP
  17526.     id AA20265 (5.65b/2.10/CWI-Amsterdam); Fri, 22 May 1992 03:19:45 +0200
  17527. Received: by mcsun.EU.net via EUnet
  17528.     id AA28112 (5.65b/CWI-2.162); Fri, 22 May 1992 03:19:42 +0200
  17529. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  17530.     (5.61/UUNET-internet-primary) id AA28433; Thu, 21 May 92 21:18:23 -0400
  17531. Received: from amc-gw.UUCP by uunet.uu.net with UUCP/RMAIL
  17532.     (queueing-rmail) id 211737.24503; Thu, 21 May 1992 21:17:37 EDT
  17533. Received: by amc-gw.amc.com (5.61/3.1.15/Ultrix-3.0)
  17534.     id AA06073; Thu, 21 May 92 09:20:26 -0700
  17535. Return-Path: <dsinet!marka>
  17536. Received: by dsinet.uucp (/\=-/\ Smail3.1.18.1 #18.22)
  17537.     id <m0loFFy-000WYpC@dsinet.uucp>; Thu, 21 May 92 08:40 PDT
  17538. Message-Id: <m0loFFy-000WYpC@dsinet.uucp>
  17539. From: marka%dsinet@uunet.UU.NET (Mark Anacker)
  17540. Subject: MSDOS Python
  17541. To: python-list@cwi.nl
  17542. Date: Thu, 21 May 92 8:40:41 PDT
  17543. Mailer: Elm [revision: 64.9]
  17544.  
  17545. >
  17546. > This is just a little note telling that Mark's patches for Python
  17547. > 0.9.6 to make it compile with Turbo C (Borland 2.0) under MS-DOS are
  17548. > now available for anonymous ftp as
  17549. >
  17550. >       ftp.cwi.nl:/pub/python-dos-patch
  17551. >
  17552. > (Mark, do you have any interesting Python modules that use your pc
  17553. > module?  Would you care to post them to the list as examples?)
  17554.  
  17555. Actually, no. I have a lot of ideas, but right now it's more of a solution
  17556. in search of a problem :-)
  17557.  
  17558. To anyone who plans on using the pcmodule - I'm planning on totally revising
  17559. that module into three parts.  There will be a "pc" module that will have
  17560. Intel CPU functions (inp, outp, etc), a "bios" module that will have an
  17561. interface to all of the BIOS functions, and a "dos" module that will contain
  17562. MS-DOS functions.  Of course, programs using these won't port to anything
  17563. besides an IBM-PC clone, but they'll at least be there so you can play with
  17564. the system.
  17565.  
  17566. I'm also starting on making a PC version of the socket module, using the
  17567. Waterloo TCP libraries.  This one I want to make a duplicate of the unix
  17568. module, so socket-based programs will port straight across (well,
  17569. within reason - still no forks in MSDOS).
  17570.  
  17571. --
  17572. Mark Anacker                            ...{!dsinet,!toybox}!marka
  17573. Digital Systems International, Inc.     Redmond, WA   USA    (206) 881-7544
  17574. "We have a massive leadership vacuum in this country...
  17575.     and we need to change bags" - Sen. Belfry
  17576. 
  17577. 
  17578. To: samborn@sunrise.com (Kevin Samborn)
  17579. Subject: Re: please remove my name from this list 
  17580. In-reply-to: Your message of "Thu, 21 May 1992 13:14:00 MDT."
  17581.              <m0loGig-00045qC@astro.sunrise.com> 
  17582. From: Guido van Rossum <guido@cwi.nl>
  17583. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17584. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17585. Date: Fri, 22 May 1992 08:28:23 +0200
  17586. Sender: guido
  17587.  
  17588. >I am leaving Sunrise to move back to my home town and get married...
  17589.  
  17590. Congratulations!  It has been nice talking to you...
  17591.  
  17592. >until then, please remove my name
  17593.  
  17594. Consider it done.
  17595.  
  17596. Cheers, stay in touch!
  17597.  
  17598. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17599. "You're so *digital*, girl!" -- Neneh Cherry
  17600. 
  17601. 
  17602. Received: from lucid.com by charon.cwi.nl with SMTP
  17603.     id AA00899 (5.65b/2.10/CWI-Amsterdam); Fri, 22 May 1992 16:25:38 +0200
  17604. Received: from aspen.lucid (aspen.lucid.com) by heavens-gate.lucid.com id AA03069g; Fri, 22 May 92 07:17:50 PDT
  17605. Received: by aspen.lucid (4.1/SMI-4.1)
  17606.     id AA21409; Fri, 22 May 92 07:25:39 PDT
  17607. Date: Fri, 22 May 92 07:25:39 PDT
  17608. From: dussud%aspen@lucid.com (Patrick Dussud)
  17609. Message-Id: <9205221425.AA21409@aspen.lucid>
  17610. To: marka%dsinet@uunet.UU.NET
  17611. Cc: python-list@cwi.nl
  17612. In-Reply-To: <m0loFFy-000WYpC@dsinet.uucp> (marka%dsinet@uunet.UU.NET)
  17613. Subject: MSDOS Python
  17614.  
  17615.  
  17616. By the way, did you try to run testall? A friend of mine says that the grammar
  17617. part never terminates on his machine. 
  17618. Patrick.
  17619. 
  17620. 
  17621. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  17622.     id AA26085 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 01:30:32 +0200
  17623. From: Lance Ellinghouse <lance@markv.com>
  17624. X-Mailer: SCO System V Mail (version 3.2)
  17625. To: python-list@cwi.nl
  17626. Subject: curses interface???
  17627. Date: Thu, 25 Jun 92 16:29:09 PDT
  17628. Message-Id:  <9206251629.aa27377@hermix.markv.com>
  17629.  
  17630. Has anyone created the necessary interfaces for a curses/forms/menus (a la AT&T)
  17631. interface?
  17632.  
  17633. Lance
  17634. lance@markv.com
  17635. 
  17636. 
  17637. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  17638.     id AA26165 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 01:31:47 +0200
  17639. From: Lance Ellinghouse <lance@markv.com>
  17640. X-Mailer: SCO System V Mail (version 3.2)
  17641. To: python-list@cwi.nl
  17642. Subject: Database interfaces
  17643. Date: Thu, 25 Jun 92 16:29:54 PDT
  17644. Message-Id:  <9206251629.aa27441@hermix.markv.com>
  17645.  
  17646. What database interfaces have people put together that they would be willing
  17647. to share?
  17648.  
  17649. Thanks,
  17650. Lance Ellinghouse
  17651. lance@markv.com
  17652.  
  17653. 
  17654. 
  17655. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  17656.     id AA27990 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 02:25:13 +0200
  17657. From: Lance Ellinghouse <lance@markv.com>
  17658. X-Mailer: SCO System V Mail (version 3.2)
  17659. To: python-list@cwi.nl
  17660. Subject: python ported to SCO UNIX?
  17661. Date: Thu, 25 Jun 92 17:23:35 PDT
  17662. Message-Id:  <9206251723.aa00188@hermix.markv.com>
  17663.  
  17664. Has anyone ported python to SCO UNIX?
  17665.  
  17666. Lance Ellinghouse
  17667. lance@markv.com
  17668. 
  17669. 
  17670. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  17671.     id AA29721 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 03:51:43 +0200
  17672. From: Lance Ellinghouse <lance@markv.com>
  17673. X-Mailer: SCO System V Mail (version 3.2)
  17674. To: python-list@cwi.nl
  17675. Subject: Re: Porting to SCO ODT
  17676. Date: Thu, 25 Jun 92 18:50:40 PDT
  17677. Message-Id:  <9206251850.aa02935@hermix.markv.com>
  17678.  
  17679. Got it to compile with minimal problems..
  17680. The tests cannot be run "out of the box" due
  17681. to 14 char truncation and things get overwritten with
  17682. comnpiled versions and such..
  17683.  
  17684. I have no idea if it is working correctly at this time.
  17685. I have to go rebuild the original versions of the tests,
  17686. then shorten their names and edit the testall.py file.
  17687.  
  17688. The only thing I really had to do was modify the Makefile
  17689. a little and socketmodule.c to remove the AF_UNIX 
  17690. parts. (put an #ifdef and a define in the makefile
  17691. to skip them).
  17692.  
  17693. Lance
  17694. lance@markv.com
  17695. 
  17696. 
  17697. Received: from math.waterloo.edu by charon.cwi.nl with SMTP
  17698.     id AA02257 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 07:25:23 +0200
  17699. Received: from tslwat.uucp by math.waterloo.edu with uucp
  17700.     id <AA09719>; Fri, 26 Jun 92 01:25:12 -0400
  17701. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  17702.     id <m0m15eI-00056QC@research.teleride.on.ca>; Thu, 25 Jun 92 22:02 EDT
  17703. Message-Id: <m0m15eI-00056QC@research.teleride.on.ca>
  17704. From: louk@research.teleride.on.ca (Lou Kates)
  17705. Subject: deep copy
  17706. To: python-list@cwi.nl
  17707. Date: Thu, 25 Jun 92 22:02:53 EDT
  17708. X-Mailer: ELM [version 2.3 PL11]
  17709.  
  17710.  
  17711. Suppose a is  a list and we wish b  to have a copy of a such that
  17712. if a is later  modified b still  retains the original value of  a
  17713. that it had when it was copied.
  17714.  
  17715. >>> b = a
  17716.  
  17717. won't work since b just points to a; however, 
  17718.  
  17719. >>> b = a[:]
  17720.  
  17721. seems to work  as  long as the   elements of a are not themselves
  17722. lists. In this last case even this does not work.
  17723.  
  17724. Is there some easy way to copy a into b as desired?
  17725.  
  17726. -- 
  17727. Lou Kates, louk@research.teleride.on.ca
  17728. 
  17729. 
  17730. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  17731.     id AA06711 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 11:33:37 +0200
  17732. Received: by schelvis.cwi.nl with SMTP; Fri, 26 Jun 1992 09:33:36 GMT
  17733. Message-Id: <9206260933.AA24624@schelvis.cwi.nl>
  17734. To: louk@research.teleride.on.ca (Lou Kates)
  17735. Cc: python-list@cwi.nl
  17736. Subject: Re: deep copy 
  17737. In-Reply-To: Message by louk@research.teleride.on.ca (Lou Kates) ,
  17738.          Thu, 25 Jun 92 22:02:53 EDT , <m0m15eI-00056QC@research.teleride.on.ca> 
  17739. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  17740. Phone: +31 20 5924098(work), +31 20 6160335(home)
  17741. X-Last-Band-Seen: Solid Ground (Korsakoff, 24-6)
  17742. X-Mini-Review: Solid and heavy.
  17743. Date: Fri, 26 Jun 1992 11:33:35 +0200
  17744. From: Jack Jansen <Jack.Jansen@cwi.nl>
  17745.  
  17746. How about:
  17747. def deepcopy(p):
  17748.     if type(p) == type([]):
  17749.         p = p[:]
  17750.         for i in range(len(p)):
  17751.             p[i] = deepcopy(p[i])
  17752.     return p
  17753.  
  17754. This will copy lists and keep references to all other objects.
  17755. Extending it to also copy other mutable objects like dictionaries is
  17756. left as an excercise to the reader:-)
  17757.  
  17758. And, of course, Guido will probably post a much more elegant and
  17759. efficient solution:-)
  17760. --
  17761. Jack Jansen        | If I can't dance I don't want to be part of
  17762. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  17763. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  17764. 
  17765. 
  17766. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17767.     id AA07185 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 11:46:26 +0200
  17768. Received: by voorn.cwi.nl with SMTP; Fri, 26 Jun 1992 09:46:27 GMT
  17769. Message-Id: <9206260946.AA23337@voorn.cwi.nl>
  17770. To: python-list@cwi.nl
  17771. Subject: Re: deep copy
  17772. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17773. In-Reply-To: Your message of "Fri, 26 Jun 1992 11:33:35 MDT."
  17774.              <9206260933.AA24624@schelvis.cwi.nl>
  17775. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17776. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17777. Date: Fri, 26 Jun 1992 11:46:26 +0200
  17778. Sender: Guido.van.Rossum@cwi.nl
  17779.  
  17780. Jack posts a simple deepcopy() function for lists just like I would
  17781. have written it, and adds:
  17782.  
  17783. >And, of course, Guido will probably post a much more elegant and
  17784. >efficient solution:-)
  17785.  
  17786. No, I won't, since the problem is impossible to solve in full
  17787. generality.
  17788.  
  17789. A "simple" problems is how to treat objects that contain references to
  17790. themselves (maybe indirectly).  This can be solved by keeping a list
  17791. of all references encountered during the copying.  But what to do with
  17792. instances of user-defined classes, or worse, instances of opaque
  17793. built-in data types -- the latter may be defined by dynamically loaded
  17794. extension modules written in C, so you can't know in advance how to
  17795. handle them all.
  17796.  
  17797. Even types that you do know about beforehand can cause complications
  17798. -- how would you define a deep copy on an open file or a window object?
  17799.  
  17800. I must say that I can't remember that the general version of the
  17801. problem has ever come up in all the tens of thousands of lines of
  17802. Python code I've written -- one level of copying is usually
  17803. sufficient.  Lou, were you thinking of a particular application or
  17804. were you just trying to find out what the limits of the language are?
  17805.  
  17806. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17807. "Many of my best friends are lumberjacks and only a few of them are
  17808. transvestites"
  17809. 
  17810. 
  17811. Replied: Fri, 26 Jun 1992 14:13:58 +0200
  17812. Replied: "python-list@cwi.nl "
  17813. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  17814.     id AA09409 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 13:46:22 +0200
  17815. Received: from stun4g.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  17816.     id AA12658; Fri, 26 Jun 92 04:40:50 PDT
  17817. Date: Fri, 26 Jun 92 04:40:50 PDT
  17818. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  17819. Message-Id: <9206261140.AA12658@jove.cs.caltech.edu>
  17820. Received: by stun4g.cs.caltech.edu (4.1/SMI-4.1)
  17821.     id AA12547; Fri, 26 Jun 92 04:46:20 PDT
  17822. To: python-list@cwi.nl
  17823. Subject: default environment setup
  17824.  
  17825. I would like to initialize a dictionary upon invocation of the python
  17826. interpreter . Is there a .python file ?
  17827.  
  17828. I am using the dictionary to index into the periodic table of elements
  17829. so that during the day instead of grunging through a textbook , I can
  17830. type
  17831.  
  17832. x = element['hydrogen']
  17833. print data_of[x]
  17834.  
  17835. where element = {'hydrogen':1, 'helium':2}
  17836.  
  17837. --
  17838. terrence brannon
  17839. brannon@jove.cs.caltech.edu 
  17840.  
  17841. 
  17842. 
  17843. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17844.     id AA09822 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 14:13:58 +0200
  17845. Received: by voorn.cwi.nl with SMTP; Fri, 26 Jun 1992 12:14:00 GMT
  17846. Message-Id: <9206261214.AA24223@voorn.cwi.nl>
  17847. To: python-list@cwi.nl
  17848. Subject: Re: default environment setup 
  17849. In-Reply-To: Your message of "Fri, 26 Jun 1992 04:40:50 MDT."
  17850.              <9206261140.AA12658@jove.cs.caltech.edu> 
  17851. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17852. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17853. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17854. Date: Fri, 26 Jun 1992 14:13:59 +0200
  17855. Sender: Guido.van.Rossum@cwi.nl
  17856.  
  17857. >I would like to initialize a dictionary upon invocation of the python
  17858. >interpreter . Is there a .python file ?
  17859.  
  17860. No, but you can simulate it by typing this at the first prompt:
  17861.  
  17862. >>> execfile('.python')
  17863.  
  17864. (This assumes the ".python" file is in the current directory.)
  17865.  
  17866. The Python interpreter does not automatically read a file of commands
  17867. because Python scripts should be independent of their environment, as
  17868. much as possible ($PYTHONPATH being the exception).
  17869.  
  17870. But if enough people want this, it's easy to add a feature whereby a
  17871. command line option (e.g. -f file) specifies a filename to be executed
  17872. before reading commands from stdin, or whereby an environment variable
  17873. (e.g. $PYTHONSTARTUP) specifies a file that is read by every
  17874. *interactive* interpreter.
  17875.  
  17876. Or are there any better ideas?
  17877.  
  17878. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17879. "It's not much of a cheese shop really, is it?"
  17880. 
  17881. 
  17882. Replied: Fri, 26 Jun 1992 15:01:25 +0200
  17883. Replied: "python-list@cwi.nl "
  17884. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  17885.     id AA10341 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 14:50:53 +0200
  17886. Received: from stun4g.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  17887.     id AA12691; Fri, 26 Jun 92 05:45:20 PDT
  17888. Date: Fri, 26 Jun 92 05:45:20 PDT
  17889. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  17890. Message-Id: <9206261245.AA12691@jove.cs.caltech.edu>
  17891. Received: by stun4g.cs.caltech.edu (4.1/SMI-4.1)
  17892.     id AA12551; Fri, 26 Jun 92 05:50:50 PDT
  17893. To: python-list@cwi.nl
  17894. Subject: although the manual states ... 
  17895.  
  17896. ------- Start of forwarded message -------
  17897. # this program
  17898.  
  17899. element = {'h': 1, 'he': 2, 'li':3, 'be':4, 'b':5}
  17900. print 'Atoms.py loaded and ready'
  17901.  
  17902. data = [ [0 , 0, '0'] , [1, 1.008, '1s1'] ,  [2, 4.003, '1s2'] ]
  17903.  
  17904.  
  17905. def lookup ( name ) :
  17906.     element[name]
  17907.  
  17908. print element
  17909. print data
  17910. fp = open('TEST', 'w')
  17911. try:
  17912.     fp.write(element)
  17913.     fp.write(data)
  17914.  
  17915. finally:
  17916.     fp.close()
  17917.  
  17918.  
  17919. # yield this output althought the manual states that lists and
  17920. # dictionaries are valid as long they are not recursive
  17921.  
  17922.  
  17923.  
  17924. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  17925. >>> import mywrite
  17926. Atoms.py loaded and ready
  17927. {'he': 2, 'b': 5, 'h': 1, 'be': 4, 'li': 3}
  17928. [[0, 0, '0'], [1, 1.008, '1s1'], [2, 4.003, '1s2']]
  17929. TypeError: illegal argument type for built-in operation
  17930. Stack backtrace (innermost last):
  17931.   File "<stdin>", line 1
  17932.   File "./mywrite.py", line 14
  17933.     fp.write(element)
  17934. >>> 
  17935. ------- End of forwarded message -------
  17936. 
  17937. 
  17938. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  17939.     id AA10424 (5.65b/2.10/CWI-Amsterdam); Fri, 26 Jun 1992 15:01:26 +0200
  17940. Received: by voorn.cwi.nl with SMTP; Fri, 26 Jun 1992 13:01:27 GMT
  17941. Message-Id: <9206261301.AA24397@voorn.cwi.nl>
  17942. To: python-list@cwi.nl
  17943. Subject: Re: although the manual states ... 
  17944. In-Reply-To: Your message of "Fri, 26 Jun 1992 05:45:20 MDT."
  17945.              <9206261245.AA12691@jove.cs.caltech.edu> 
  17946. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  17947. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  17948. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  17949. Date: Fri, 26 Jun 1992 15:01:26 +0200
  17950. Sender: Guido.van.Rossum@cwi.nl
  17951.  
  17952. Terrence M. Brannon complains that "fp.write()" does not accept
  17953. dictionaries and lists, even though the manual states that these can
  17954. be written unless recursive.
  17955.  
  17956. Unfortunately, this is the case only for the "print" statement, not
  17957. for the "write()" method of file objects -- the latter only accepts
  17958. strings.  To write non-string objects to a file, you can do two
  17959. things:
  17960.  
  17961. (1) temporarily assign the file object to sys.stdout and use print:
  17962.  
  17963.     >>> f = open(..., 'w')
  17964.     >>> import sys
  17965.     >>> save_stdout = sys.stdout
  17966.     >>> sys.stdout = f
  17967.     >>> print element
  17968.     >>> print data
  17969.     >>> sys.stdout = save_stdout
  17970.  
  17971. (2) convert the objects to a string using reverse quotes:
  17972.  
  17973.     >>> f = open(..., 'w')
  17974.     >>> f.write(`element`)
  17975.     >>> f.write('\n')
  17976.     >>> f.write(`data`)
  17977.     >>> f.write('\n')
  17978.  
  17979. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  17980. "Look matey, this parrot wouldn't voom if I put four thousand volts
  17981. through it"
  17982. 
  17983. 
  17984. Replied: Mon, 29 Jun 1992 11:46:37 +0200
  17985. Replied: "python-list "
  17986. Replied: Mon, 29 Jun 1992 11:06:15 +0200
  17987. Replied: "python-list@cwi.nl "
  17988. Received: from math.waterloo.edu by charon.cwi.nl with SMTP
  17989.     id AA06851 (5.65b/2.10/CWI-Amsterdam); Sat, 27 Jun 1992 06:25:20 +0200
  17990. Received: from tslwat.uucp by math.waterloo.edu with uucp
  17991.     id <AA09522>; Sat, 27 Jun 92 00:25:08 -0400
  17992. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  17993.     id <m0m1TJk-0001KgC@research.teleride.on.ca>; Fri, 26 Jun 92 23:19 EDT
  17994. Message-Id: <m0m1TJk-0001KgC@research.teleride.on.ca>
  17995. Date: Fri, 26 Jun 92 23:19 EDT
  17996. From: louk@research.teleride.on.ca (Lou Kates)
  17997. To: python-list@cwi.nl
  17998. Subject: Re: deep copy
  17999.  
  18000.  
  18001. Forwarded message:
  18002. > From:    Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18003. > I must say that I can't remember that the general version of the
  18004. > problem has ever come up in all the tens of thousands of lines of
  18005. > Python code I've written -- one level of copying is usually
  18006. > sufficient.  Lou, were you thinking of a particular application or
  18007. > were you just trying to find out what the limits of the language are?
  18008.  
  18009. I was  working  on an  application which requires assigning (i.e.
  18010. deep copying) a two dimensional array  (i.e. a list of lists). As
  18011. an  aside,   it  also  requires  adding a  one dimensional  array
  18012. elementwise to a  one  dimensional  slice  of  a two  dimensional
  18013. array.
  18014.  
  18015. In general, my mindset is APL-ish here and I would like to assign
  18016. (i.e. deep copy) multi-dimensional arrays. (Actually I would like
  18017. to do  other  APL style  operations as well including elementwise
  18018. operations   such   as  addition   and  subtraction  on pairs  of
  18019. multidimensional, or at least one and two dimensional arrays.)
  18020.  
  18021. At least some of these  items (such as assigning multidimensional
  18022. arrays in a deep copy sense) seem as fundamental to me as strings
  18023. and dictionaries.
  18024.  
  18025. I assume that the applications you  refer to above were primarily
  18026. systems  software    oriented  whereas  I  am  referring more  to
  18027. mathematical,  statistical  and   data   processing     sorts  of
  18028. applications.
  18029.  
  18030. -- 
  18031. Lou Kates, louk@research.teleride.on.ca
  18032.  
  18033. 
  18034. 
  18035. Replied: Mon, 29 Jun 1992 11:05:02 +0200
  18036. Replied: "Lance Ellinghouse <lance@markv.com> "
  18037. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  18038.     id AA02058 (5.65b/2.10/CWI-Amsterdam); Sat, 27 Jun 1992 18:34:57 +0200
  18039. From: Lance Ellinghouse <lance@markv.com>
  18040. X-Mailer: SCO System V Mail (version 3.2)
  18041. To: python-list@cwi.nl
  18042. Subject: select 
  18043. Date: Sat, 27 Jun 92 9:33:10 PDT
  18044. Message-Id:  <9206270933.aa09839@hermix.markv.com>
  18045.  
  18046. Has anyone created a select function similar to the select in BSD sockets?
  18047.  
  18048. I figured you could pass a list  of items in and have it return 
  18049. a 3 element tupple of the return values.. 
  18050.  
  18051. Lance
  18052. lance@markv.com
  18053. 
  18054. 
  18055. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  18056.     id AA02064 (5.65b/2.10/CWI-Amsterdam); Sat, 27 Jun 1992 18:38:04 +0200
  18057. From: Lance Ellinghouse <lance@markv.com>
  18058. X-Mailer: SCO System V Mail (version 3.2)
  18059. To: python-list@cwi.nl
  18060. Subject: Python...
  18061. Date: Sat, 27 Jun 92 9:36:25 PDT
  18062. Message-Id:  <9206270936.aa09848@hermix.markv.com>
  18063.  
  18064. What are people using python for in "real life"?
  18065. Any production usages?
  18066.  
  18067. I am thinking of using it for a project myself but cannot
  18068. decide between using Tcl or python. It will be a highly 
  18069. interactive program that also needs to be resource conserving
  18070. because up to 64 people will be using it at once an a single 
  18071. machine.
  18072.  
  18073. Thanks,
  18074. Lance Ellinghouse
  18075. lance@markv.com
  18076.  
  18077. P.S. Please fogive if I ask some REALLY dump questions..
  18078.  
  18079. 
  18080. 
  18081. Received: from [138.95.19.12] by charon.cwi.nl with SMTP
  18082.     id AA22376 (5.65b/2.10/CWI-Amsterdam); Sun, 28 Jun 1992 23:03:26 +0200
  18083. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  18084.     id AA16849; Sun, 28 Jun 92 13:57:08 -0700
  18085. Received: by eng2.sequent.com (5.65/1.34)
  18086.     id AA09209; Sun, 28 Jun 92 13:58:58 -0700
  18087. Message-Id: <9206282058.AA09209@eng2.sequent.com>
  18088. To: Lance Ellinghouse <lance@markv.com>
  18089. Cc: python-list@cwi.nl
  18090. Subject: Re: select 
  18091. Priority: Normal
  18092. Precedence: first-class
  18093. Organization: Sequent Computer Systems, Inc.
  18094.               Decision Support Group - MailStop: RHE2-358
  18095.           15450 S.W. Koll Parkway
  18096.               Beaverton, OR  97006
  18097. X-Phone: (503) 578-4404
  18098. X-Fax: (503) 578-7569
  18099. X-Uucp: ...!uunet!sequent!jaap
  18100. X-Internet: jaap@sequent.com
  18101. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  18102.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  18103.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  18104. In-Reply-To: Lance Ellinghouse's message of Sat, 27 Jun 92 09:33:10 -0700.
  18105.              <9206270933.aa09839@hermix.markv.com> 
  18106. Date: Sun, 28 Jun 92 13:58:57 PDT
  18107. From: Jaap Vermeulen <jaap@sequent.com>
  18108.  
  18109.  
  18110. | Has anyone created a select function similar to the select in BSD sockets?
  18111. | I figured you could pass a list  of items in and have it return 
  18112. | a 3 element tupple of the return values.. 
  18113.  
  18114. Not yet, but I was thinking more in the direction of the poll() SYSV
  18115. system call.  It provides nicer semantics than the bitmask directed
  18116. select().
  18117.  
  18118. It would look like:
  18119.  
  18120.     file1 = open ('...', 'r')
  18121.     file2 = open ('...', 'r')
  18122.     timeout = 0
  18123.  
  18124.     filelist = select (((file1, 'r'), (file2, 'r')), timeout)
  18125.  
  18126. The input would be a list of (fileobject, string) pairs.  The string
  18127. would indicate 'r', 'w', 'e', or any permutation of those letters (for
  18128. read, write and exception; if you really want to confuse people you
  18129. could change the 'e' into an 'x' :-) :-).  The timeout would be in some
  18130. convenient timeformat with enough granularity to satisfy most needs
  18131. (tenth of seconds?).  If the timeout is 'None' the call would be
  18132. blocking.  It will return a list of (fileobject, string) pairs for
  18133. those fileobjects that have one or multiple events pending, or 'None'
  18134. if there are no pending events on any of the fileobjects.
  18135.  
  18136. If you have poll(), the implementation is quite straight-forward.  If
  18137. you only have select(), you have to construct the bitmasks from the
  18138. filelist information (which shouldn't be too hard).
  18139.  
  18140. Just my $0.02 worth.  Good luck,
  18141.  
  18142.     -Jaap-
  18143. --
  18144. Jaap Vermeulen                    +--------------------------+
  18145.                         | Sequent Computer Systems |
  18146.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  18147.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  18148. 
  18149. 
  18150. Replied: Mon, 29 Jun 1992 11:12:38 +0200
  18151. Replied: "python-list "
  18152. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  18153.     id AA22727 (5.65b/2.10/CWI-Amsterdam); Sun, 28 Jun 1992 23:56:52 +0200
  18154. From: Lance Ellinghouse <lance@markv.com>
  18155. X-Mailer: SCO System V Mail (version 3.2)
  18156. To: jaap@sequent.com
  18157. Subject: Re: select
  18158. Cc: python-list@cwi.nl
  18159. Date: Sun, 28 Jun 92 14:55:33 PDT
  18160. Message-Id:  <9206281455.aa13522@hermix.markv.com>
  18161.  
  18162. > | Has anyone created a select function similar to the select in BSD sockets?
  18163. > | 
  18164. > | I figured you could pass a list  of items in and have it return 
  18165. > | a 3 element tupple of the return values.. 
  18166. > Not yet, but I was thinking more in the direction of the poll() SYSV
  18167. > system call.  It provides nicer semantics than the bitmask directed
  18168. > select().
  18169. > It would look like:
  18170. >     file1 = open ('...', 'r')
  18171. >     file2 = open ('...', 'r')
  18172. >     timeout = 0
  18173. >     filelist = select (((file1, 'r'), (file2, 'r')), timeout)
  18174. > The input would be a list of (fileobject, string) pairs.  The string
  18175. > would indicate 'r', 'w', 'e', or any permutation of those letters (for
  18176. > read, write and exception; if you really want to confuse people you
  18177. > could change the 'e' into an 'x' :-) :-).  
  18178.  
  18179. 'e' sounds reasonable as does the layout of the call.
  18180. The question is how do you specify you wish to select/poll on it
  18181. for both reading and writing? Remember, sockets are bi-directional.
  18182. Or would you prefer that you do a sock.makefile('r') on it first?
  18183. Actually that seems like a nice solution to that. Forget the statement
  18184. above. I answered my own question.
  18185.  
  18186. > The timeout would be in some
  18187. > convenient timeformat with enough granularity to satisfy most needs
  18188. > (tenth of seconds?).  If the timeout is 'None' the call would be
  18189. > blocking.
  18190.  
  18191. I like that. But you should make it milliseconds since that is what
  18192. the select() and poll() actually take. (Although for the project I am
  18193. working on, I will probably only set it for 5-10 MINUTES :) )
  18194.  
  18195. > It will return a list of (fileobject, string) pairs for
  18196. > those fileobjects that have one or multiple events pending, or 'None'
  18197. > if there are no pending events on any of the fileobjects.
  18198.  
  18199. Sounds reasonable. Clean, clear, easy to parse return!
  18200.  
  18201. > If you have poll(), the implementation is quite straight-forward.  If
  18202. > you only have select(), you have to construct the bitmasks from the
  18203. > filelist information (which shouldn't be too hard).
  18204.  
  18205. The problem with poll() is that on some platforms, it handles
  18206. sockets, pipes, and files differently than select(). But I think
  18207. that the C code should be able to select which one you wish
  18208. to use via a #define statement when you do the compile.
  18209.  
  18210. Lance Ellinghouse
  18211. lance@markv.com
  18212.  
  18213. 
  18214. 
  18215. Received: from [138.95.19.12] by charon.cwi.nl with SMTP
  18216.     id AA25422 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 00:15:27 +0200
  18217. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  18218.     id AA18536; Sun, 28 Jun 92 15:13:31 -0700
  18219. Received: by eng2.sequent.com (5.65/1.34)
  18220.     id AA09779; Sun, 28 Jun 92 15:15:22 -0700
  18221. Message-Id: <9206282215.AA09779@eng2.sequent.com>
  18222. To: Lance Ellinghouse <lance@markv.com>
  18223. Cc: python-list@cwi.nl
  18224. Subject: Re: select 
  18225. Priority: Normal
  18226. Precedence: first-class
  18227. Organization: Sequent Computer Systems, Inc.
  18228.               Decision Support Group - MailStop: RHE2-358
  18229.           15450 S.W. Koll Parkway
  18230.               Beaverton, OR  97006
  18231. X-Phone: (503) 578-4404
  18232. X-Fax: (503) 578-7569
  18233. X-Uucp: ...!uunet!sequent!jaap
  18234. X-Internet: jaap@sequent.com
  18235. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  18236.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  18237.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  18238. In-Reply-To: Lance Ellinghouse's message of Sun, 28 Jun 92 14:55:33 -0700.
  18239.              <9206281455.aa13522@hermix.markv.com> 
  18240. Date: Sun, 28 Jun 92 15:15:21 PDT
  18241. From: Jaap Vermeulen <jaap@sequent.com>
  18242.  
  18243.  
  18244. | > The input would be a list of (fileobject, string) pairs.  The string
  18245. | > would indicate 'r', 'w', 'e', or any permutation of those letters (for
  18246. | > read, write and exception; if you really want to confuse people you
  18247. | > could change the 'e' into an 'x' :-) :-).  
  18248. | 'e' sounds reasonable as does the layout of the call.
  18249. | The question is how do you specify you wish to select/poll on it
  18250. | for both reading and writing? Remember, sockets are bi-directional.
  18251.  
  18252. If 'it' is opened for reading and writing, just specify the string 'rw'.
  18253.  
  18254. | Or would you prefer that you do a sock.makefile('r') on it first?
  18255. | Actually that seems like a nice solution to that. Forget the statement
  18256. | above. I answered my own question.
  18257.  
  18258. Correct, for sockets you have to create file fileobject before being able
  18259. to select on it.
  18260.  
  18261. | I like that. But you should make it milliseconds since that is what
  18262. | the select() and poll() actually take. (Although for the project I am
  18263. | working on, I will probably only set it for 5-10 MINUTES :) )
  18264.  
  18265. The question is not what the underlying system call can handle, the
  18266. question is what makes sense from python's point of view.  :-)  If
  18267. anybody ever needs millisecond granularity, sure, use that.  However I
  18268. doubt that it is useful...
  18269.  
  18270. | The problem with poll() is that on some platforms, it handles
  18271. | sockets, pipes, and files differently than select(). But I think
  18272. | that the C code should be able to select which one you wish
  18273. | to use via a #define statement when you do the compile.
  18274.  
  18275. Well, I assumed the select() semantics.  That is, you can select on
  18276. anything (if possible; as you point out some systems have
  18277. limitations).  If you don't select for an exception, and an exception
  18278. occurs for one of the fileobjects, you get a python error back
  18279. (probably with the value EBADF).  Note that this is different from the
  18280. sysV poll() system call semantics...
  18281.  
  18282.     -Jaap-
  18283. --
  18284. Jaap Vermeulen                    +--------------------------+
  18285.                         | Sequent Computer Systems |
  18286.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  18287.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  18288. 
  18289. 
  18290. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  18291.     id AA11133 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 10:47:58 +0200
  18292. Received: by schelvis.cwi.nl with SMTP; Mon, 29 Jun 1992 08:47:57 GMT
  18293. Message-Id: <9206290847.AA29275@schelvis.cwi.nl>
  18294. To: Lance Ellinghouse <lance@markv.com>
  18295. Cc: python-list@cwi.nl
  18296. Subject: Re: Python... 
  18297. In-Reply-To: Message by Lance Ellinghouse <lance@markv.com> ,
  18298.          Sat, 27 Jun 92 9:36:25 PDT , <9206270936.aa09848@hermix.markv.com> 
  18299. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  18300. Phone: +31 20 5924098(work), +31 20 6160335(home)
  18301. X-Last-Band-Seen: Kevin Coyne (Vondelpark, 28-6)
  18302. X-Mini-Review: Good! He does not look too healthy in the sun, though...
  18303. Date: Mon, 29 Jun 1992 10:47:57 +0200
  18304. From: Jack Jansen <Jack.Jansen@cwi.nl>
  18305.  
  18306. Well, here at CWI we use python for *many* real life projects. Cmifed,
  18307. our multi-media document editor, is written completely in python. It
  18308. is about 10K lines, so that should probably make it the biggest python
  18309. program around as well.
  18310.  
  18311. There's also meeting, a network audio conferencing program, and
  18312. various tools to capture, playback and edit audio and video.
  18313.  
  18314. Besides those there's lots of utilities like an audible mail-warner (a
  18315. different sample played for everyone who sends you mail), some helper
  18316. programs for the ethernet radio stuff, etc.
  18317. --
  18318. Jack Jansen        | If I can't dance I don't want to be part of
  18319. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  18320. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  18321. 
  18322. 
  18323. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18324.     id AA11480 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 11:06:14 +0200
  18325. Received: by voorn.cwi.nl with SMTP; Mon, 29 Jun 1992 09:06:16 GMT
  18326. Message-Id: <9206290906.AA27838@voorn.cwi.nl>
  18327. To: python-list@cwi.nl
  18328. Subject: Re: select 
  18329. In-Reply-To: Your message of "Sat, 27 Jun 1992 09:33:10 MDT."
  18330.              <9206270933.aa09839@hermix.markv.com> 
  18331. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18332. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18333. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18334. Date: Mon, 29 Jun 1992 11:06:15 +0200
  18335. Sender: Guido.van.Rossum@cwi.nl
  18336.  
  18337. >Has anyone created a select function similar to the select in BSD sockets?
  18338. >
  18339. >I figured you could pass a list  of items in and have it return 
  18340. >a 3 element tupple of the return values.. 
  18341.  
  18342. What a coincidence!  About a week ago Jack and I sat together and
  18343. designed an interface for BSD select, and Jack made an implementation
  18344. of it.  The interface is like this:
  18345.  
  18346.     import select
  18347.     (ready_r, ready_w, ready_e) = \
  18348.         select.select(test_r, test_w, test_e, timeout)
  18349.  
  18350. Here test_r, test_w and test_e are lists of file or socket objects
  18351. (actually, anything with a 'fileno' method that returns a file
  18352. descriptor is possible; alternately, you may specify file descriptors
  18353. directly) and ready_{r,w,e} are similar lists containing the subset of
  18354. items from the corresponding test_* lists that are ready within the
  18355. timeout.  The timeout is a floating point number in seconds (so you
  18356. can specify any fraction you like and the system will round it to the
  18357. available precision).  Leaving the timeout parameter out means an
  18358. infinite time-out.
  18359.  
  18360. I can post or mail the patches if you like; it requires changes to
  18361. socketmodule.c and fileobject.c to add 'fileno()' methods to them.
  18362.  
  18363. I read the discussion on this list about an alternate design; while
  18364. that is also elegant I don't think the difference is important enough
  18365. to change the interface now that it's already been implemented...
  18366.  
  18367. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18368. "Rule six -- there is *no* rule six"
  18369.  
  18370. [Sorry Lance -- I accidentally mailed you a private copy of this...]
  18371. 
  18372. 
  18373. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18374.     id AA11517 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 11:12:40 +0200
  18375. Received: by voorn.cwi.nl with SMTP; Mon, 29 Jun 1992 09:12:41 GMT
  18376. Message-Id: <9206290912.AA27857@voorn.cwi.nl>
  18377. To: python-list@cwi.nl
  18378. Subject: Re: select 
  18379. In-Reply-To: Your message of "Sun, 28 Jun 1992 14:55:33 MDT."
  18380.              <9206281455.aa13522@hermix.markv.com> 
  18381. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18382. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18383. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18384. Date: Mon, 29 Jun 1992 11:12:41 +0200
  18385. Sender: Guido.van.Rossum@cwi.nl
  18386.  
  18387. (See also my previous message where I explain that Jack Jansen has
  18388. just created a BSD select interface.)
  18389.  
  18390. >The question is how do you specify you wish to select/poll on it
  18391. >for both reading and writing? Remember, sockets are bi-directional.
  18392.  
  18393. I'd say specify it twice, once for reading and once for writing.  This
  18394. works with Jack's implementation.  Or is there a limitation to poll
  18395. that makes this impossible?
  18396.  
  18397. >I like that. But you should make it milliseconds since that is what
  18398. >the select() and poll() actually take. (Although for the project I am
  18399. >working on, I will probably only set it for 5-10 MINUTES :) )
  18400.  
  18401. The standard solution in Python for this is floating point.  I still
  18402. want to change the time.sleep() function to accept a floating point
  18403. number so I can get rid of the separate time.millisleep() function --
  18404. the module time predates the existence of floating point in Python...
  18405.  
  18406. >The problem with poll() is that on some platforms, it handles
  18407. >sockets, pipes, and files differently than select(). But I think
  18408. >that the C code should be able to select which one you wish
  18409. >to use via a #define statement when you do the compile.
  18410.  
  18411. In the event that I or someone else may have to create an
  18412. implementation of Jack's select.select() using SYSV poll(), can you
  18413. (or another SYSV guru) enlighten me as to the nature of these
  18414. differences?
  18415.  
  18416. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18417. "What a senseless waste of human life"
  18418. 
  18419. 
  18420. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18421.     id AA11964 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 11:46:38 +0200
  18422. Received: by voorn.cwi.nl with SMTP; Mon, 29 Jun 1992 09:46:40 GMT
  18423. Message-Id: <9206290946.AA27927@voorn.cwi.nl>
  18424. To: python-list@cwi.nl
  18425. Subject: Re: deep copy 
  18426. In-Reply-To: Your message of "Fri, 26 Jun 1992 23:19:00 MDT."
  18427.              <m0m1TJk-0001KgC@research.teleride.on.ca> 
  18428. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18429. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18430. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18431. Date: Mon, 29 Jun 1992 11:46:39 +0200
  18432. Sender: Guido.van.Rossum@cwi.nl
  18433.  
  18434. >I was  working  on an  application which requires assigning (i.e.
  18435. >deep copying) a two dimensional array  (i.e. a list of lists). As
  18436. >an  aside,   it  also  requires  adding a  one dimensional  array
  18437. >elementwise to a  one  dimensional  slice  of  a two  dimensional
  18438. >array.
  18439. >
  18440. >In general, my mindset is APL-ish here and I would like to assign
  18441. >(i.e. deep copy) multi-dimensional arrays. (Actually I would like
  18442. >to do  other  APL style  operations as well including elementwise
  18443. >operations   such   as  addition   and  subtraction  on pairs  of
  18444. >multidimensional, or at least one and two dimensional arrays.)
  18445. >
  18446. >At least some of these  items (such as assigning multidimensional
  18447. >arrays in a deep copy sense) seem as fundamental to me as strings
  18448. >and dictionaries.
  18449. >
  18450. >I assume that the applications you  refer to above were primarily
  18451. >systems  software    oriented  whereas  I  am  referring more  to
  18452. >mathematical,  statistical  and   data   processing     sorts  of
  18453. >applications.
  18454.  
  18455. Yes, I'm afraid that's the general mindset from which Python
  18456. originated.  If you really need a lot of 2-or-more dimensional array
  18457. operations you may be better off adding a new type to the language.
  18458. The type could use the interface style used by dictionaries (the
  18459. general term being "mappings") and it could define additional methods
  18460. for slice operations.  Buzz, buzz, whirr, whirr (thinking aloud :-)
  18461. you might use it as follows:
  18462.  
  18463. >>> import matrix
  18464. >>> m = matrix.new(10, 10, 0.0) # Create a 10x10 matrix initialized with zeros
  18465. >>> print m.dim()
  18466. (10, 10)
  18467. >>> print m.keys()
  18468. [(0, 0), (0, 1), ..., (0, 9), (1, 0), (1, 1), ..., (9, 9)]
  18469. >>> for i in range(m.dim()[0]): m[i, i] = 1.0
  18470. >>> for row, col in m.keys(): print row, col, m[row, col]
  18471. 0 0 1.0
  18472. 0 1 0.0
  18473. .
  18474. .
  18475. .
  18476. 9 9 1.0
  18477. >>> m2 = m # Usual pointer copy -- m2 is the same object as m
  18478. >>> m3 = m.copy() # creates a new object -- elements are still shared
  18479. >>> m4 = m.slice((0, 10), (5, 10)) # creates a new object
  18480.                                    # containing rows [0..10), cols [5, 10)
  18481. >>> print m4.dim()
  18482. (10, 5)
  18483. >>> print m4.keys()
  18484. [(0, 0), (0, 1), ..., (0, 4), (1, 0), (1, 1), ..., (9, 4)]
  18485. >>> # Note how the indexes always start at 0 (point of discussion though)
  18486. >>> m.setslice((0, 10), (0, 5), m4) # Copy right half (m4) to left half
  18487.  
  18488. Vector extracted from a matrix are lists:
  18489.  
  18490. >>> v1 = m.row(0)
  18491. >>> print v1
  18492. [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  18493. >>> v2 = m.col(1)
  18494. >>> v3 = m.vec(0, 2) # same as m.row(2)
  18495. >>> v4 = m.vec(1, 3) # same as m.col(3)
  18496. >>> m.setrow(4, v3)
  18497. >>> m.setcol(5, v4)
  18498. >>> m.setvec(0, 6, v1)
  18499. >>> m.setvec(1, 7, v2)
  18500.  
  18501. Dot and other products are simply added.
  18502.  
  18503. I'm not sure what needs to be generalized to make this work for higher
  18504. dimensional arrays.  Sparse arrays are another matter.  One
  18505. interface would make references to non-existing elements illegal, and
  18506. require the user to use keys() or has_key() to figure out whether an
  18507. element exists; another interface would keep the sparseness internal
  18508. to the implementation and return a default value.
  18509.  
  18510. Note that there is no reason why elements of matrixes should be
  18511. numbers, except when they are used by numerical operations.
  18512.  
  18513. The language currently makes it hard to support element-wise addition
  18514. etc. using the standard notation (m1+m2 etc.) but it might evolve --
  18515. although + is already defined for lists to mean concatenation.
  18516.  
  18517. Other ideas?
  18518.  
  18519. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18520. "It's not much of a cheese shop really, is it?"
  18521. 
  18522. 
  18523. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  18524.     id AA13791 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 13:46:36 +0200
  18525. Received: by schelvis.cwi.nl with SMTP; Mon, 29 Jun 1992 11:46:35 GMT
  18526. Message-Id: <9206291146.AA29935@schelvis.cwi.nl>
  18527. To: python-list@cwi.nl
  18528. Subject: Database interface to python
  18529. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  18530. Phone: +31 20 5924098(work), +31 20 6160335(home)
  18531. X-Last-Band-Seen: Kevin Coyne (Vondelpark, 28-6)
  18532. X-Mini-Review: Good! He does not look too healthy in the sun, though...
  18533. Date: Mon, 29 Jun 1992 13:46:34 +0200
  18534. From: Jack Jansen <Jack.Jansen@cwi.nl>
  18535.  
  18536. Somebody asked for a database interface for python recently, and since
  18537. I had a spare afternoon (and wanted the exercise anyway:-) I made a
  18538. python interface to the unix ndbm(3) database library. The interface
  18539. is a mapping type, so you use it just as if dbm databases were python
  18540. dictionaries (except for opening the database, of course). I'll mail
  18541. it out if anyone is interested.
  18542. --
  18543. Jack Jansen        | If I can't dance I don't want to be part of
  18544. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  18545. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  18546. 
  18547. 
  18548. Replied: Mon, 29 Jun 1992 15:55:40 +0200
  18549. Replied: "python-list "
  18550. Received: from math.waterloo.edu by charon.cwi.nl with SMTP
  18551.     id AA15620 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 15:26:51 +0200
  18552. Received: from tslwat.uucp by math.waterloo.edu with uucp
  18553.     id <AA00701>; Mon, 29 Jun 92 09:26:38 -0400
  18554. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  18555.     id <m0m2Khg-0001M2C@research.teleride.on.ca>; Mon, 29 Jun 92 08:19 EDT
  18556. Message-Id: <m0m2Khg-0001M2C@research.teleride.on.ca>
  18557. Date: Mon, 29 Jun 92 08:19 EDT
  18558. From: louk@research.teleride.on.ca (Lou Kates)
  18559. To: python-list@cwi.nl
  18560. Subject: range()
  18561. Cc: louk@research.teleride.on.ca
  18562.  
  18563.  
  18564. In Python, you currently have to say:
  18565.  
  18566.     for i in range(len(a)-1,-1,-1):
  18567.  
  18568. to iterate backwards over the indices  of a list. Range should be
  18569. defined so that you can simply say:
  18570.  
  18571.     for i in range(len(a),0,-1):
  18572.  
  18573. If k>0, this could be done by defining
  18574.      
  18575.     range(m,n,-k)  
  18576.      
  18577. to be equivalent to 
  18578.  
  18579.     range(n,m,k).reverse()
  18580.  
  18581. That is to say, if the third  argument is negative then the range
  18582. that is  produced includes the second argument but not the first.
  18583. If the third argument  is positive  the range includes the  first
  18584. argument but not the second as is currently the case.
  18585.  
  18586.  
  18587. Lou Kates, louk@research.teleride.on.ca
  18588. 
  18589. 
  18590. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18591.     id AA16097 (5.65b/2.10/CWI-Amsterdam); Mon, 29 Jun 1992 15:55:44 +0200
  18592. Received: by voorn.cwi.nl with SMTP; Mon, 29 Jun 1992 13:55:42 GMT
  18593. Message-Id: <9206291355.AA28526@voorn.cwi.nl>
  18594. To: python-list@cwi.nl
  18595. Subject: Re: range() 
  18596. In-Reply-To: Your message of "Mon, 29 Jun 1992 08:19:00 MDT."
  18597.              <m0m2Khg-0001M2C@research.teleride.on.ca> 
  18598. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18599. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18600. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18601. Date: Mon, 29 Jun 1992 15:55:41 +0200
  18602. Sender: Guido.van.Rossum@cwi.nl
  18603.  
  18604. >In Python, you currently have to say:
  18605. >
  18606. >    for i in range(len(a)-1,-1,-1):
  18607. >
  18608. >to iterate backwards over the indices  of a list. Range should be
  18609. >defined so that you can simply say:
  18610. >
  18611. >    for i in range(len(a),0,-1):
  18612. >
  18613. >If k>0, this could be done by defining
  18614. >     
  18615. >    range(m,n,-k)  
  18616. >     
  18617. >to be equivalent to 
  18618. >
  18619. >    range(n,m,k).reverse()
  18620. >
  18621. >That is to say, if the third  argument is negative then the range
  18622. >that is  produced includes the second argument but not the first.
  18623. >If the third argument  is positive  the range includes the  first
  18624. >argument but not the second as is currently the case.
  18625.  
  18626. Interesting suggestion; at first I thought I'd agree with you
  18627. completely (barring backward compatibility, which I don't think will
  18628. be a big problem); but what about things like range(0, -5, -1)?
  18629. Currently this returns [0, -1, -2, -3, -4]; under Lou's proposed
  18630. definition it would change to [-1, -2, -3, -4, -5].  Intimate lovers
  18631. of two's complement arithmetic might expect that, but to me it feels
  18632. slightly uneasy.
  18633.  
  18634. Any other opinions?
  18635.  
  18636. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18637. "This is an ex-parrot"
  18638. 
  18639. 
  18640. Replied: Tue, 30 Jun 1992 10:50:16 +0200
  18641. Replied: "python-list@cwi.nl "
  18642. Received: from math.waterloo.edu by charon.cwi.nl with SMTP
  18643.     id AA10315 (5.65b/2.10/CWI-Amsterdam); Tue, 30 Jun 1992 04:24:53 +0200
  18644. Received: from tslwat.uucp by math.waterloo.edu with uucp
  18645.     id <AA13152>; Mon, 29 Jun 92 22:24:49 -0400
  18646. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  18647.     id <m0m2X5k-0001JsC@research.teleride.on.ca>; Mon, 29 Jun 92 21:33 EDT
  18648. Message-Id: <m0m2X5k-0001JsC@research.teleride.on.ca>
  18649. From: louk@research.teleride.on.ca (Lou Kates)
  18650. Subject: Re: range() (fwd)
  18651. To: python-list@cwi.nl
  18652. Date: Mon, 29 Jun 92 21:33:11 EDT
  18653. X-Mailer: ELM [version 2.3 PL11]
  18654.  
  18655. Forwarded message:
  18656. > Sender: Guido.van.Rossum@cwi.nl
  18657. > >In Python, you currently have to say:
  18658. > >
  18659. > >    for i in range(len(a)-1,-1,-1):
  18660. > >
  18661. > >to iterate backwards over the indices  of a list. Range should be
  18662. > >defined so that you can simply say:
  18663. > >
  18664. > >    for i in range(len(a),0,-1):
  18665. > >
  18666. > >If k>0, this could be done by defining
  18667. > >     
  18668. > >    range(m,n,-k)  
  18669. > >     
  18670. > >to be equivalent to 
  18671. > >
  18672. > >    range(n,m,k).reverse()
  18673. > >
  18674. > >That is to say, if the third  argument is negative then the range
  18675. > >that is  produced includes the second argument but not the first.
  18676. > >If the third argument  is positive  the range includes the  first
  18677. > >argument but not the second as is currently the case.
  18678. > Interesting suggestion; at first I thought I'd agree with you
  18679. > completely (barring backward compatibility, which I don't think will
  18680. > be a big problem); but what about things like range(0, -5, -1)?
  18681. > Currently this returns [0, -1, -2, -3, -4]; under Lou's proposed
  18682. > definition it would change to [-1, -2, -3, -4, -5].  Intimate lovers
  18683. > of two's complement arithmetic might expect that, but to me it feels
  18684. > slightly uneasy.
  18685. > Any other opinions?
  18686. > --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18687. > "This is an ex-parrot"
  18688.  
  18689.  
  18690. You make  an interesting  point. I   would like to expand on  it.
  18691. Suppose s is  scalar and v is a list regarded  as a vector. As in
  18692. usual vector arithmetic define s * v to be the list, i.e. vector,
  18693. whose i-th element is s * v[i].
  18694.  
  18695. Then the existing range() function in Python enjoys the following
  18696. pleasing proportionality property:
  18697.  
  18698.     s * range(m, n, k) = range(s * m, s * n, s * k)
  18699.  
  18700. for all non-zero values of s including negative values.
  18701.  
  18702. The definition  that I  had proposed  is the same as the existing
  18703. range() for positive steps and  by definition obeys the following
  18704. identity which allows it to be extended to negative steps:
  18705.  
  18706.     range(m, n, -k) = range(n, m, k).reverse()
  18707.  
  18708. With the   existing range(),  the formula   for s * range() as  a
  18709. range()  is   simple   even   if   s<0  but    the   formula  for
  18710. range().reverse() as a range() is complex.
  18711.  
  18712. In my  proposed  formula, the  formula for range().reverse() as a
  18713. range() is simple but the formula for  s * range() as a range  is
  18714. complex if s<0.
  18715.  
  18716. Its a  judgement call as to  whether range() should behave nicely
  18717. under proportionality or  reversal since it appears that it won't
  18718. behave nicely under both.
  18719.  
  18720. I have to admit that I  prefer the proportionality identity which
  18721. means that range() would stay as it is.
  18722.  
  18723. Getting back  to the  original example,  I guess the moral of the
  18724. story  is   that  if one   wants to  elegantly express  backwards
  18725. iteration over list indices one should use the idiom:
  18726.  
  18727.     for i in range(len(a)).reverse():
  18728.  
  18729. Lou
  18730.  
  18731. P.S. Note how   the  above   discussion required APL-like  vector
  18732.      multiplication to state the proportionality property even in
  18733.      this apparently not heavily mathematical setting!
  18734.  
  18735. -- 
  18736. Lou Kates, louk@research.teleride.on.ca
  18737. 
  18738. 
  18739. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18740.     id AA16726 (5.65b/2.10/CWI-Amsterdam); Tue, 30 Jun 1992 10:50:17 +0200
  18741. Received: by voorn.cwi.nl with SMTP; Tue, 30 Jun 1992 08:50:18 GMT
  18742. Message-Id: <9206300850.AA29730@voorn.cwi.nl>
  18743. To: python-list@cwi.nl
  18744. Subject: Re: range() (fwd) 
  18745. In-Reply-To: Your message of "Mon, 29 Jun 1992 21:33:11 MDT."
  18746.              <m0m2X5k-0001JsC@research.teleride.on.ca> 
  18747. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18748. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18749. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18750. Date: Tue, 30 Jun 1992 10:50:17 +0200
  18751. Sender: Guido.van.Rossum@cwi.nl
  18752.  
  18753. Lou Kates writes:
  18754.  
  18755. >Its a  judgement call as to  whether range() should behave nicely
  18756. >under proportionality or  reversal since it appears that it won't
  18757. >behave nicely under both.
  18758. >
  18759. >I have to admit that I  prefer the proportionality identity which
  18760. >means that range() would stay as it is.
  18761.  
  18762. Well, the law of the least hassle also suggests that it should stay
  18763. that way, unless someone else will start a major upraising...
  18764.  
  18765. >Getting back  to the  original example,  I guess the moral of the
  18766. >story  is   that  if one   wants to  elegantly express  backwards
  18767. >iteration over list indices one should use the idiom:
  18768. >
  18769. >    for i in range(len(a)).reverse():
  18770.  
  18771. Unfortunately, list.reverse() is a procedure (== function returning
  18772. None) that has a side effect (reversing the list in place) and does
  18773. not return a value, so in real current Python you have to write:
  18774.  
  18775.     tmp = range(len(a))
  18776.     tmp.reverse()
  18777.     for i in tmp: ...
  18778.  
  18779. In general, Python procedures can't be changed to functions without
  18780. breaking existing code since the interpreter prints the value of an
  18781. expression-statement if it isn't otherwise used.  If this were deemed
  18782. useful enough it would be easy to add a method list.reversed() that
  18783. would return a reversed copy.  For consistency we would then also need
  18784. a list.sorted() method.
  18785.  
  18786. >P.S. Note how   the  above   discussion required APL-like  vector
  18787. >     multiplication to state the proportionality property even in
  18788. >     this apparently not heavily mathematical setting!
  18789.  
  18790. "Required" seems a bit strong here, especially from someone who has
  18791. already admitted he's got an APL mindset. :-)
  18792.  
  18793. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18794. "It happens sometimes -- people just explode.  Natural causes."
  18795. 
  18796. 
  18797. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18798.     id AA21135 (5.65b/2.10/CWI-Amsterdam); Tue, 30 Jun 1992 14:08:32 +0200
  18799. Received: by voorn.cwi.nl with SMTP; Tue, 30 Jun 1992 12:08:32 GMT
  18800. Message-Id: <9206301208.AA00506@voorn.cwi.nl>
  18801. To: python-list@cwi.nl
  18802. Subject: dbm and select modules available
  18803. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18804. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18805. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18806. Date: Tue, 30 Jun 1992 14:08:31 +0200
  18807. Sender: Guido.van.Rossum@cwi.nl
  18808.  
  18809. On popular demand I've made the 'dbm' and 'select' modules available
  18810. for anonymous ftp.
  18811.  
  18812. Host: ftp.cwi.nl
  18813. Directory: pub
  18814. File: python-dbm-select
  18815.  
  18816. It's a shar file containing some patches and new source files.
  18817.  
  18818. (Note that this is not a release of Python 0.9.7; it's a set of
  18819. temporary patches for those who asked for them.)
  18820.  
  18821. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18822. Founder of the Royal Society for Prevention of Cruelty to Amoebae
  18823. 
  18824. 
  18825. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  18826.     id AA29544 (5.65b/2.10/CWI-Amsterdam); Tue, 30 Jun 1992 20:53:12 +0200
  18827. From: Lance Ellinghouse <lance@markv.com>
  18828. X-Mailer: SCO System V Mail (version 3.2)
  18829. To: python-list@cwi.nl
  18830. Subject: Submitted files and such..
  18831. Date: Tue, 30 Jun 92 11:53:33 PDT
  18832. Message-Id:  <9206301153.aa11416@hermix.markv.com>
  18833.  
  18834. For those submitting files and such (now and in the future)
  18835. can you please keep in mind that some of us have machines that
  18836. only support 14 char file names...
  18837. The posted python-ndbm-select file contains a number of files that
  18838. I have to pull apart by hand due to the length of the file names.
  18839.  
  18840. Thank you,
  18841. Lance Ellinghouse
  18842. lance@markv.com
  18843.  
  18844. 
  18845. 
  18846. Replied: Wed, 01 Jul 1992 10:40:52 +0200
  18847. Replied: Lance Ellinghouse <lance@markv.com>
  18848. Replied: python-list@cwi.nl
  18849. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  18850.     id AA17661 (5.65b/2.10/CWI-Amsterdam); Wed, 1 Jul 1992 03:59:13 +0200
  18851. From: Lance Ellinghouse <lance@markv.com>
  18852. X-Mailer: SCO System V Mail (version 3.2)
  18853. To: python-list@cwi.nl
  18854. Subject: selectmodule.c & mkvalue()
  18855. Date: Tue, 30 Jun 92 19:00:13 PDT
  18856. Message-Id:  <9206301900.aa29340@hermix.markv.com>
  18857.  
  18858. the recently posted socketmodule.c uses a routine (mkvalue())
  18859. that is not defined anywhere.. Could the person that created
  18860. socketmodule.c please post mkvalue() also? 
  18861.  
  18862. Thank you,
  18863. Lance Ellinghouse
  18864. lance@markv.com
  18865. 
  18866. 
  18867. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18868.     id AA24397 (5.65b/2.10/CWI-Amsterdam); Wed, 1 Jul 1992 10:40:46 +0200
  18869. Received: by voorn.cwi.nl with SMTP; Wed, 1 Jul 1992 08:40:50 GMT
  18870. Message-Id: <9207010840.AA03023@voorn.cwi.nl>
  18871. To: Lance Ellinghouse <lance@markv.com>
  18872. Cc: python-list@cwi.nl
  18873. Subject: Re: selectmodule.c & mkvalue() 
  18874. In-Reply-To: Your message of "Tue, 30 Jun 1992 19:00:13 MDT."
  18875.              <9206301900.aa29340@hermix.markv.com> 
  18876. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18877. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18878. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18879. Date: Wed, 01 Jul 1992 10:40:49 +0200
  18880. Sender: Guido.van.Rossum@cwi.nl
  18881.  
  18882. >the recently posted socketmodule.c uses a routine (mkvalue())
  18883. >that is not defined anywhere.. Could the person that created
  18884. >socketmodule.c please post mkvalue() also? 
  18885.  
  18886. Oops, it's in a new version of modsupport.c.  I've put that on the ftp
  18887. site as well (just the file).  You must add a declaration for
  18888. mkvalue() to modsupport.h: "object *mkvalue();" will do.
  18889.  
  18890. BTW, I apologize for the less than professional way in which these
  18891. mods are distributed.  It's really time that I send out an entire new
  18892. version of the system (0.9.7) but that takes much more time -- I want
  18893. to avoid the kind of mistakes in a real distribution that I made now :-)
  18894.  
  18895. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18896. PS: do you save your mail without headers and signatures?
  18897. 
  18898. 
  18899. Replied: Thu, 02 Jul 1992 16:41:24 +0200
  18900. Replied: "brannon@jove.cs.caltech.edu (Terrence M. Brannon) python-list@cwi.nl"
  18901. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  18902.     id AA05285 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 16:35:45 +0200
  18903. Received: from stun4r.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  18904.     id AA26955; Thu, 2 Jul 92 07:35:29 PDT
  18905. Date: Thu, 2 Jul 92 07:35:29 PDT
  18906. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  18907. Message-Id: <9207021435.AA26955@jove.cs.caltech.edu>
  18908. Received: by stun4r.cs.caltech.edu (4.1/SMI-4.1)
  18909.     id AA04130; Thu, 2 Jul 92 07:35:35 PDT
  18910. To: python-list@cwi.nl
  18911. Subject: HOw does one print without a c/r ?
  18912. Comments: Hyperbole mail buttons accepted, v3.02P.
  18913.  
  18914. Is it possible to print something but not automatically have a
  18915. carriage return appended to it ?
  18916.  
  18917. --
  18918. terrence brannon
  18919. brannon@jove.cs.caltech.edu 
  18920.  
  18921. 
  18922. 
  18923. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18924.     id AA05349 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 16:41:23 +0200
  18925. Received: by voorn.cwi.nl with SMTP; Thu, 2 Jul 1992 14:41:26 GMT
  18926. Message-Id: <9207021441.AA07454@voorn.cwi.nl>
  18927. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  18928. Cc: python-list@cwi.nl
  18929. Subject: Re: HOw does one print without a c/r ? 
  18930. In-Reply-To: Your message of "Thu, 02 Jul 1992 07:35:29 MDT."
  18931.              <9207021435.AA26955@jove.cs.caltech.edu> 
  18932. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18933. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18934. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  18935. Date: Thu, 02 Jul 1992 16:41:25 +0200
  18936. Sender: Guido.van.Rossum@cwi.nl
  18937.  
  18938. >Is it possible to print something but not automatically have a
  18939. >carriage return appended to it ?
  18940.  
  18941. Yes, append a comma after the last argument to print.  For instance,
  18942. this loop prints the numbers 0..9 on a line separated by spaces.  Note
  18943. the parameterless "print" that adds the final newline:
  18944.  
  18945. >>> for i in range(10):
  18946. ...     print i,
  18947. ... else:
  18948. ...     print
  18949. ... 
  18950. 0 1 2 3 4 5 6 7 8 9
  18951. >>> 
  18952.  
  18953. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  18954. "Is it a bird? No! Is it a plane? No! It's bicycle repair man!"
  18955. 
  18956. 
  18957. Replied: Thu, 02 Jul 1992 17:05:21 +0200
  18958. Replied: "python-list "
  18959. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  18960.     id AA05464 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 16:54:34 +0200
  18961. Received: from stun4r.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  18962.     id AA26984; Thu, 2 Jul 92 07:53:49 PDT
  18963. Date: Thu, 2 Jul 92 07:53:49 PDT
  18964. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  18965. Message-Id: <9207021453.AA26984@jove.cs.caltech.edu>
  18966. Received: by stun4r.cs.caltech.edu (4.1/SMI-4.1)
  18967.     id AA04133; Thu, 2 Jul 92 07:53:55 PDT
  18968. To: python-list@cwi.nl
  18969. Subject: One Really Important Suggestion
  18970. Comments: Hyperbole mail buttons accepted, v3.02P.
  18971.  
  18972. NOt to be critical, but since python is an interpreted language , a
  18973. key asset of such a language would be to re-import modules and be able
  18974. to test the methods out that I just changed.
  18975.  
  18976. It would speed up incremental development much more if I did not have
  18977. to exit the interpreter each time I wanted to retest an altered
  18978. module.
  18979.  
  18980. However, I guess an alternate solution is to run the source from the
  18981. shell instead of import module and the module.method()
  18982.  
  18983. --
  18984. terrence brannon
  18985. brannon@jove.cs.caltech.edu 
  18986.  
  18987. 
  18988. 
  18989. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  18990.     id AA05584 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 17:05:20 +0200
  18991. Received: by voorn.cwi.nl with SMTP; Thu, 2 Jul 1992 15:05:23 GMT
  18992. Message-Id: <9207021505.AA07573@voorn.cwi.nl>
  18993. To: python-list@cwi.nl
  18994. Subject: Re: One Really Important Suggestion 
  18995. In-Reply-To: Your message of "Thu, 02 Jul 1992 07:53:49 MDT."
  18996.              <9207021453.AA26984@jove.cs.caltech.edu> 
  18997. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  18998. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  18999. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  19000. Date: Thu, 02 Jul 1992 17:05:22 +0200
  19001. Sender: Guido.van.Rossum@cwi.nl
  19002.  
  19003. >Not to be critical, but since python is an interpreted language , a
  19004.                                             ^^^^^^^^^^^
  19005. >key asset of such a language would be to re-import modules and be able
  19006. >to test the methods out that I just changed.
  19007.  
  19008. I suppose you meant "interactive" there?
  19009.  
  19010. >It would speed up incremental development much more if I did not have
  19011. >to exit the interpreter each time I wanted to retest an altered
  19012. >module.
  19013.  
  19014. You don't have to.  Look at the description of the built-in function
  19015. 'reload()'.  There are some restrictions, but if you are working on a
  19016. single it is often what you need.
  19017.  
  19018. >However, I guess an alternate solution is to run the source from the
  19019. >shell instead of import module and the module.method()
  19020.  
  19021. If you have GNU Emacs, and are using the Python mode provided with the
  19022. Python distribution (misc/python-mode.el), you can use C-c C-c in a
  19023. Python buffer to run the current buffer through the interpreter.  You
  19024. can also start an interactive Python interpreter in another window
  19025. with C-c !; C-c C-c will then send the buffer as input to that
  19026. interpreter.  See the mode-specific info for more hints.
  19027.  
  19028. Finally, the '-c statements' command line option to the interpreter is
  19029. sometimes useful, especially if your shell has a fast way of repeating
  19030. the last command (e.g. csh '!!'  or ksh 'r').
  19031.  
  19032. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  19033. "It's a bit runny, sir"
  19034. 
  19035. 
  19036. Replied: Thu, 02 Jul 1992 22:48:58 +0200
  19037. Replied: python-list@cwi.nl
  19038. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  19039.     id AA11418 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 22:18:08 +0200
  19040. Received: from stun4q.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  19041.     id AA28196; Thu, 2 Jul 92 13:17:56 PDT
  19042. Date: Thu, 2 Jul 92 13:17:56 PDT
  19043. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19044. Message-Id: <9207022017.AA28196@jove.cs.caltech.edu>
  19045. Received: by stun4q.cs.caltech.edu (4.1/SMI-4.1)
  19046.     id AA12118; Thu, 2 Jul 92 13:18:02 PDT
  19047. To: python-list@cwi.nl
  19048. Subject: what is the 256 on my screen ?
  19049. Comments: Hyperbole mail buttons accepted, v3.02P.
  19050.  
  19051. i did an os.system() call and I keep getting thsse 256 or 0 on my
  19052. screen after the call . Is there anyway to nullify this output ?
  19053.  
  19054. i am starting up a faq for python by the way
  19055.  
  19056. --
  19057. terrence brannon
  19058. brannon@jove.cs.caltech.edu 
  19059.  
  19060. 
  19061. 
  19062. Received: from [138.95.19.12] by charon.cwi.nl with SMTP
  19063.     id AA12043 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 22:39:38 +0200
  19064. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  19065.     id AA15603; Thu, 2 Jul 92 13:37:38 -0700
  19066. Received: by eng2.sequent.com (5.65/1.34)
  19067.     id AA02958; Thu, 2 Jul 92 13:39:32 -0700
  19068. Message-Id: <9207022039.AA02958@eng2.sequent.com>
  19069. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19070. Cc: python-list@cwi.nl
  19071. Subject: Re: what is the 256 on my screen ? 
  19072. Priority: Normal
  19073. Precedence: first-class
  19074. Organization: Sequent Computer Systems, Inc.
  19075.               Decision Support Group - MailStop: RHE2-358
  19076.           15450 S.W. Koll Parkway
  19077.               Beaverton, OR  97006
  19078. X-Phone: (503) 578-4404
  19079. X-Fax: (503) 578-7569
  19080. X-Uucp: ...!uunet!sequent!jaap
  19081. X-Internet: jaap@sequent.com
  19082. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  19083.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  19084.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  19085. In-Reply-To: Terrence M. Brannon's message of Thu, 02 Jul 92 13:17:56 -0700.
  19086.              <9207022017.AA28196@jove.cs.caltech.edu> 
  19087. Date: Thu, 02 Jul 92 13:39:31 PDT
  19088. From: Jaap Vermeulen <jaap@sequent.com>
  19089.  
  19090.  
  19091. | i did an os.system() call and I keep getting thsse 256 or 0 on my
  19092. | screen after the call . Is there anyway to nullify this output ?
  19093.  
  19094. It's the returned exitcode.  I guess you could use:
  19095.  
  19096.     None = os.system('...')
  19097.  
  19098.     -Jaap-
  19099. --
  19100. Jaap Vermeulen                    +--------------------------+
  19101.                         | Sequent Computer Systems |
  19102.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  19103.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  19104. 
  19105. 
  19106. To: python-list@cwi.nl
  19107. Subject: Re: what is the 256 on my screen ? 
  19108. In-reply-to: Your message of "Thu, 02 Jul 1992 13:17:56 MDT."
  19109.              <9207022017.AA28196@jove.cs.caltech.edu> 
  19110. From: Guido van Rossum <guido@cwi.nl>
  19111. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  19112. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  19113. Date: Thu, 02 Jul 1992 22:48:56 +0200
  19114. Sender: guido
  19115.  
  19116. brannon@jove.cs.caltech.edu (Terrence M. Brannon):
  19117. >i did an os.system() call and I keep getting thsse 256 or 0 on my
  19118. >screen after the call . Is there anyway to nullify this output ?
  19119.  
  19120. Yes.  This is the exit status of the command (really the return value
  19121. of system(3)).  The Python interpreter prints the value of an
  19122. expression statement unless it is None -- this is done so you can use
  19123. the interpreter as an interactive calculator.  The simplest way of
  19124. avoiding the printing of the value is to assign it to a dummy value.
  19125.  
  19126. >i am starting up a faq for python by the way
  19127.  
  19128. That's great.  Will it be ftp'able?  I might point new readers of the
  19129. list to it, to get them up at full speed quickly...
  19130.  
  19131. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  19132. "Repo Man's got all night, *every* night"
  19133. 
  19134. 
  19135. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  19136.     id AA12221 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 22:48:55 +0200
  19137. Received: by voorn.cwi.nl with SMTP; Thu, 2 Jul 1992 20:48:57 GMT
  19138. Message-Id: <9207022048.AA08441@voorn.cwi.nl>
  19139. To: python-list@cwi.nl
  19140. Subject: Re: what is the 256 on my screen ? 
  19141. In-Reply-To: Your message of "Thu, 02 Jul 1992 13:17:56 MDT."
  19142.              <9207022017.AA28196@jove.cs.caltech.edu> 
  19143. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  19144. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  19145. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  19146. Date: Thu, 02 Jul 1992 22:48:56 +0200
  19147. Sender: Guido.van.Rossum@cwi.nl
  19148.  
  19149. brannon@jove.cs.caltech.edu (Terrence M. Brannon):
  19150. >i did an os.system() call and I keep getting thsse 256 or 0 on my
  19151. >screen after the call . Is there anyway to nullify this output ?
  19152.  
  19153. Yes.  This is the exit status of the command (really the return value
  19154. of system(3)).  The Python interpreter prints the value of an
  19155. expression statement unless it is None -- this is done so you can use
  19156. the interpreter as an interactive calculator.  The simplest way of
  19157. avoiding the printing of the value is to assign it to a dummy value.
  19158.  
  19159. >i am starting up a faq for python by the way
  19160.  
  19161. That's great.  Will it be ftp'able?  I might point new readers of the
  19162. list to it, to get them up at full speed quickly...
  19163.  
  19164. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  19165. "Repo Man's got all night, *every* night"
  19166. 
  19167. 
  19168. Received: from hopscotch.ksr.com by charon.cwi.nl with SMTP
  19169.     id AA12423 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 22:53:23 +0200
  19170. Received: from ksr.com (frankenstein.ksr.com) by hopscotch.ksr.com with SMTP
  19171.     id AA07013; Thu, 2 Jul 1992 16:52:24 -0400
  19172. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  19173.     id AA04275; Thu, 2 Jul 92 16:53:10 EDT
  19174. Received: by kaos.ksr.com (4.0/KSR-2.0)
  19175.     id AA21371; Thu, 2 Jul 92 16:53:09 EDT
  19176. Message-Id: <9207022053.AA21371@kaos.ksr.com>
  19177. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19178. Subject: Re: what is the 256 on my screen ?
  19179. Cc: python-list@cwi.nl
  19180. Date: Thu, 02 Jul 92 16:53:07 -0400
  19181. From: Tim Peters <tim@ksr.com>
  19182.  
  19183. >  i did an os.system() call and I keep getting thsse 256 or 0 on my
  19184. >  screen after the call . Is there anyway to nullify this output ?
  19185. >
  19186. >  i am starting up a faq for python by the way
  19187.  
  19188. Well, since Python isn't Perl, when it does something you didn't expect
  19189. you can bet it did it for a rational reason <grin>.
  19190.  
  19191. Specifically, os.system(some_cmd) returns the exit status of `some_cmd',
  19192. and Python always prints the value of a statement that consists in its
  19193. entirety of a raw expression.  So the line
  19194.  
  19195.     os.system(some_cmd)
  19196.  
  19197. will always print the exit status of some_cmd, for exactly the same
  19198. reason that the line
  19199.  
  19200.     1+2
  19201.  
  19202. prints "3".  Try, e.g.,
  19203.  
  19204.     suppress_printing_exit_status = os.system(some_cmd)
  19205.  
  19206. That won't print the exit status, & for the same reason that
  19207.  
  19208.     a = 1+2
  19209.  
  19210. won't print "3".
  19211.  
  19212. Was that pedantic enough <grin>?
  19213.  
  19214. there's-always-a-sensible-method-to-guido's-fine-madness-ly y'rs  - tim
  19215.  
  19216. Tim Peters   Kendall Square Research Corp
  19217. tim@ksr.com,         ksr!tim@uunet.uu.net
  19218. 
  19219. 
  19220. Received: from [138.95.19.12] by charon.cwi.nl with SMTP
  19221.     id AA13673 (5.65b/2.10/CWI-Amsterdam); Thu, 2 Jul 1992 23:12:02 +0200
  19222. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  19223.     id AA16690; Thu, 2 Jul 92 14:10:01 -0700
  19224. Received: by eng2.sequent.com (5.65/1.34)
  19225.     id AA07452; Thu, 2 Jul 92 14:11:54 -0700
  19226. Message-Id: <9207022111.AA07452@eng2.sequent.com>
  19227. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19228. Cc: python-list@cwi.nl
  19229. Subject: [Jaap: Re: what is the 256 on my screen ?] 
  19230. Priority: Normal
  19231. Precedence: first-class
  19232. Date: Thu, 02 Jul 92 14:11:53 PDT
  19233. From: Jaap Vermeulen <jaap@sequent.com>
  19234.  
  19235.  
  19236. I said:
  19237.  
  19238. |It's the returned exitcode.  I guess you could use:
  19239. |
  19240. |    None = os.system('...')
  19241.  
  19242. Ah, that would redefine 'None', not what you want ... :-)
  19243.  
  19244. I thought it would just throw away the value that way (Guido, wouldn't
  19245. it be handy if there were a way to just discard a value?)
  19246.  
  19247.     -Jaap-
  19248. 
  19249. 
  19250. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  19251.     id AA15777 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 00:03:53 +0200
  19252. From: Lance Ellinghouse <lance@markv.com>
  19253. X-Mailer: SCO System V Mail (version 3.2)
  19254. To: python-list@cwi.nl
  19255. Subject: FTP sites...
  19256. Date: Thu, 2 Jul 92 15:04:40 PDT
  19257. Message-Id:  <9207021504.ab02327@hermix.markv.com>
  19258.  
  19259. > >i am starting up a faq for python by the way
  19260. > That's great.  Will it be ftp'able?  I might point new readers of the
  19261. > list to it, to get them up at full speed quickly...
  19262.  
  19263. To make things easier, there needs to be an FTP archive site for
  19264. the latest versions of Python as well as additional modules,
  19265. FAQ, etc...
  19266.  
  19267. Does anyone want to set one up in the US? I would be happy to, but our bandwidth
  19268. it not very much (14.4K)...
  19269.  
  19270. Lance Ellinghouse
  19271. lance@markv.com
  19272.  
  19273. 
  19274. 
  19275. Replied: Fri, 03 Jul 1992 11:07:54 +0200
  19276. Replied: "brannon@jove.cs.caltech.edu (Terrence M. Brannon) python-list@cwi.nl"
  19277. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  19278.     id AA21644 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 01:32:54 +0200
  19279. Received: from stun4q.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  19280.     id AA28413; Thu, 2 Jul 92 16:32:48 PDT
  19281. Date: Thu, 2 Jul 92 16:32:48 PDT
  19282. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19283. Message-Id: <9207022332.AA28413@jove.cs.caltech.edu>
  19284. Received: by stun4q.cs.caltech.edu (4.1/SMI-4.1)
  19285.     id AA12213; Thu, 2 Jul 92 16:32:55 PDT
  19286. To: python-list@cwi.nl
  19287. Subject: just out of curiosity...
  19288. Comments: Hyperbole mail buttons accepted, v3.02P.
  19289.  
  19290. TeX section headers have the following format:
  19291.  
  19292. \section{Using Nitroglycerin in the Home}
  19293.  
  19294. the following works:
  19295.  
  19296. #!/tmp/python
  19297.  
  19298. import os, string, sys , regex
  19299.  
  19300. sect_regex = regex.compile('^.*\\section{\(.+\)+}.*$')
  19301. def main():
  19302.     p = open(sys.argv[1] , 'r')
  19303.     total, d = None, {}
  19304.     for line in p.readlines():
  19305.     if (sect_regex.match(line) != -1):
  19306.         print line
  19307.  
  19308. main()
  19309.  
  19310.  
  19311. but changing the regex to  
  19312. sect_regex = regex.compile('^\\section{\(.+\)+}.*$')
  19313.  
  19314. fails which implies that there is something at the front of the line
  19315. that I cannot see . any idea what?
  19316.  
  19317. --
  19318. terrence brannon
  19319. brannon@jove.cs.caltech.edu 
  19320.  
  19321. 
  19322. 
  19323. Received: from goudvink.cwi.nl by charon.cwi.nl with SMTP
  19324.     id AA29815 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 10:59:09 +0200
  19325. Received: by goudvink.cwi.nl with SMTP
  19326.     id AA19285 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 10:59:07 +0200
  19327. Message-Id: <9207030859.AA19285.jh@goudvink.cwi.nl>
  19328. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19329. Cc: python-list@cwi.nl
  19330. Subject: Re: just out of curiosity... 
  19331. In-Reply-To: Your message of "Thu, 02 Jul 1992 16:32:48 PDT."
  19332.              <9207022332.AA28413@jove.cs.caltech.edu> 
  19333. From: Jan-Hein Buhrman <Jan-Hein.Buhrman@cwi.nl>
  19334. X-Organization: CWI (Centre for Mathematics and Computer Science),
  19335.         Kruislaan 413, 1098 SJ  Amsterdam, The Netherlands
  19336. X-Phone: +31 20 592.4098/9333 (work), +31 20 592.4199 (fax)
  19337. Date: Fri, 03 Jul 1992 10:59:05 +0200
  19338. Sender: Jan-Hein.Buhrman@cwi.nl
  19339.  
  19340. Terence M. Brannon <brannon@jove.cs.caltech.edu>:
  19341. >Target to search for is `\section{Using Nitroglycerin in the Home}',
  19342. >at the start of a line.
  19343. >
  19344. >sect_regex = regex.compile('^.*\\section{\(.+\)+}.*$') works, but
  19345. >sect_regex = regex.compile('^\\section{\(.+\)+}.*$') doesn't work.
  19346. I'm not a regular python user (that's an understatement), but I
  19347. recognize the type of confusion from Emacs' elisp programming.
  19348.  
  19349. For python, the string '\\' contains one (1) backslash (`\'). So
  19350. regexp.compile just sees the sequence `\sectio<...etc.>', and depending
  19351. on the kind of regexp library, mode, and program you are working with,
  19352. the backslash might be thrown away, generates an error, or means "match
  19353. a character whose syntax belongs to the code `e' (see Emacs syntax
  19354. codes)."
  19355.  
  19356. I tested the next part of code, and it works:
  19357.  
  19358. sect_regex = regex.compile('^\\\\section{\\(.+\\)}.*$')
  19359.  
  19360. Watch also the double backslashes before the braces. Hope this helps.
  19361.  
  19362. -- Jan-Hein Buhrman -- CWI, Amsterdam -- <jh@cwi.nl> --
  19363. NFS server (amd:109) not responding still trying
  19364. NFS server (amd:109) ok
  19365. /ufs/jh/lib/mottos: No such file or directory
  19366. 
  19367. 
  19368. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  19369.     id AA29953 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 11:07:54 +0200
  19370. Received: by voorn.cwi.nl with SMTP; Fri, 3 Jul 1992 09:07:56 GMT
  19371. Message-Id: <9207030907.AA09686@voorn.cwi.nl>
  19372. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19373. Cc: python-list@cwi.nl
  19374. Subject: Re: just out of curiosity... 
  19375. In-Reply-To: Your message of "Thu, 02 Jul 1992 16:32:48 MDT."
  19376.              <9207022332.AA28413@jove.cs.caltech.edu> 
  19377. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  19378. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  19379. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  19380. Date: Fri, 03 Jul 1992 11:07:55 +0200
  19381. Sender: Guido.van.Rossum@cwi.nl
  19382.  
  19383. >sect_regex = regex.compile('^\\section{\(.+\)+}.*$')
  19384. >
  19385. >fails which implies that there is something at the front of the line
  19386. >that I cannot see . any idea what?
  19387.  
  19388. To include a literal backslash in a regular expression you must double
  19389. it, but to include one in a Python string literal you must also double
  19390. it.  As a consequence, you have to QUADRUPLE a literal backslash
  19391. included in a regular expression written as a Python string literal.
  19392.  
  19393. Now you may ask, "but the \( and \) worked just fine".  That is
  19394. because in Python string literals (and this is documented in the
  19395. reference manual!) a backslash followed by a character that isn't part
  19396. of one of the standard escape sequences (\n, \t, \ddd, etc. -- exactly
  19397. the same list as in Standard C) stays in the string.  As far as I know
  19398. the only character that has a special meaning following a backslash in
  19399. regular expressions as well as in Python string literals is the
  19400. backslash itself.
  19401.  
  19402. Call it a minor nuisance.  I'll add a note to the documentation of the
  19403. regex module.
  19404.  
  19405. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  19406. "This is an ex-parrot"
  19407. 
  19408. 
  19409. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19410. cc: python-list@cwi.nl
  19411. Subject: Re: just out of curiosity... 
  19412. In-reply-to: Your message of "Thu, 02 Jul 1992 16:32:48 MDT."
  19413.              <9207022332.AA28413@jove.cs.caltech.edu> 
  19414. From: Guido van Rossum <guido@cwi.nl>
  19415. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  19416. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  19417. Date: Fri, 03 Jul 1992 11:07:55 +0200
  19418. Sender: guido
  19419.  
  19420. >sect_regex = regex.compile('^\\section{\(.+\)+}.*$')
  19421. >
  19422. >fails which implies that there is something at the front of the line
  19423. >that I cannot see . any idea what?
  19424.  
  19425. To include a literal backslash in a regular expression you must double
  19426. it, but to include one in a Python string literal you must also double
  19427. it.  As a consequence, you have to QUADRUPLE a literal backslash
  19428. included in a regular expression written as a Python string literal.
  19429.  
  19430. Now you may ask, "but the \( and \) worked just fine".  That is
  19431. because in Python string literals (and this is documented in the
  19432. reference manual!) a backslash followed by a character that isn't part
  19433. of one of the standard escape sequences (\n, \t, \ddd, etc. -- exactly
  19434. the same list as in Standard C) stays in the string.  As far as I know
  19435. the only character that has a special meaning following a backslash in
  19436. regular expressions as well as in Python string literals is the
  19437. backslash itself.
  19438.  
  19439. Call it a minor nuisance.  I'll add a note to the documentation of the
  19440. regex module.
  19441.  
  19442. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  19443. "This is an ex-parrot"
  19444. 
  19445. 
  19446. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  19447.     id AA10317 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 19:11:47 +0200
  19448. Received: from stun4b.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  19449.     id AA00981; Fri, 3 Jul 92 10:11:25 PDT
  19450. Date: Fri, 3 Jul 92 10:11:25 PDT
  19451. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19452. Message-Id: <9207031711.AA00981@jove.cs.caltech.edu>
  19453. Received: by stun4b.cs.caltech.edu (4.1/SMI-4.1)
  19454.     id AA01933; Fri, 3 Jul 92 10:11:35 PDT
  19455. To: python-list@cwi.nl
  19456. Subject: Survey of Emacs Lisp, Perl, Python, and Tcl 
  19457. Comments: Hyperbole mail buttons accepted, v3.02P.
  19458.  
  19459. Note: this is not the Python FAQ. I am working on that next.
  19460.  
  19461. The purpose of this FAQ is to expose people to several languages which
  19462. can cutdown/eliminate shell programming and C programming.
  19463.  
  19464. All of these languages are quick interpreted languages which can do
  19465. things in a few lines which might take hundreds of lines of C/shell
  19466. code.
  19467.  
  19468. I tried to be objective but I have not learned Perl. I looked into Perl
  19469. but gave it up - it seemed very odd to me.
  19470.  
  19471. Suggestions to Terrence Brannon <brannon@jove.cs.caltech.edu>. 
  19472. E-mail jbw@cs.bu.edu for details about the League for Programming Freedom.
  19473.  
  19474.  
  19475. Overview
  19476. ********
  19477.  
  19478.  
  19479. Emacs Lisp
  19480. ==========
  19481. Anything Emacs does is done in Lisp, so you can do it to. But you can
  19482. only do it inside of Emacs Lisp for the most part. The two work arounds
  19483. are: (1) use emacs-server.el which allows Emacs to send/receive on
  19484. sockets (2) use emacs server mode. Both are rather hairy.
  19485.  
  19486. Cant call from C and cant call from the shell.
  19487.  
  19488.  
  19489. Perl
  19490. ====
  19491. Combines the best features of awk, sed, and shell programming. The
  19492. latest version can be embedded in C programs. It is also easy to call
  19493. from the shell. 
  19494.  
  19495.  
  19496. Python
  19497. ======
  19498. An object-oriented interpreted language. Callable from C as well as the
  19499. shell. Interpretation may be sidestepped through the use of the
  19500. byte-compiler on files. Python played a major role in testing Amoeba, a
  19501. distributed operating system developed at CWI.
  19502.  
  19503.  
  19504. Tcl
  19505. ===
  19506. Extremely strong in X-windows. It can do a 5 page x-windows program in
  19507. *2* lines and more. Callable from C and from the shell.
  19508.  
  19509. 
  19510. File: toolkit.languages.info  Node: Power, Prev: Overview, Up: Top, Next: Graphics
  19511.  
  19512. Power
  19513. *****
  19514.  
  19515. All of the systems will be evaluated in terms of their capabilities in
  19516. graphics, string handling, and process control. They will also be
  19517. evaluated in terms of their extensions.
  19518.  
  19519. 
  19520. File: toolkit.languages.info  Node: Graphics, Prev: Power, Up: Top, Next: String Handling
  19521.  
  19522. Graphics
  19523. ========
  19524.  
  19525.  
  19526. Emacs Lisp 
  19527. -----------
  19528. None internally, but the ability to spawn inferior unix
  19529. processes and issue shell commands allows it to use tcl's WAFE or fire
  19530. up a Python process and control it with ease.
  19531.  
  19532.  
  19533. Perl
  19534. ----
  19535. Again none interally, but it might take the same route os Emacs Lisp but
  19536. my feeling is it would be a harder task than in Emacs Lisp.
  19537.  
  19538.  
  19539. Python
  19540. ------
  19541. Through the use of the STDWIN paradigm, the same source code
  19542. can do graphics in the following systems:
  19543.  
  19544.   1. X-windows
  19545.   2. Macintosh
  19546.   3. Atari ST
  19547.   4. DOS
  19548.   5. Silicon Graphics SGI workstations
  19549.  
  19550. This approach allows flexibility but means that no one graphics system's
  19551. potential is maximized. 
  19552.  
  19553.  
  19554. Tcl
  19555. ---
  19556. Overloaded with all types of very very easy to do attractive
  19557. graphics in X-Windows only. Easy to learn, and has many nice widgets
  19558. added to it such as a photo widget for display of scanned PBM images,
  19559. a bar graph widget, an editable text widget and much more. 
  19560. Powerful tcl-based graphics systems come to mind:
  19561.  
  19562.   1. VOGLE - 3D rendering, fonts
  19563.   2. BYO Interface Builder. A snap to use.
  19564.   3. WAFE . Send text string from anywhere (C language, Emacs, shell) to
  19565.      a server and have the graphics done for you.
  19566.   4. BOS - An object oriented extension for Tcl which also has some
  19567.      widgets added to it.
  19568.  
  19569. 
  19570. File: toolkit.languages.info  Node: String Handling, Prev: Graphics, Up: Top, Next: Process Control
  19571.  
  19572. String Handling
  19573. ===============
  19574.  
  19575.  
  19576. Emacs Lisp
  19577. ----------
  19578. Strong. Many good string handling functions for operation on buffers as
  19579. well as string. Search replace backward and forward with regexps. Many
  19580. good string handling functions are found in tree-dired.el in gmhist*.el.
  19581. As well as in ange-ftp.el.
  19582.  
  19583.  
  19584. Perl
  19585. ----
  19586. Well, its a language optimized for this so it is good, but in some cases
  19587. the lack of complex data structures is a problem. For example, even
  19588. though Perl is becoming the tool of choice among system administrators,
  19589. Tom Christiansen, an expert Perl programmer makes note of how the lack
  19590. of complex data structures in Perl made a program to format the output
  19591. of du in a tree format exceedingly difficult. This same program was done
  19592. in Python in 50 lines. 
  19593.  
  19594.  
  19595. Python
  19596. ------
  19597. It has a string module and regexp module. It has all the string
  19598. *search* capabilities of Emacs, but since strings are immutable in
  19599. Python, it understandably does not have Emacs Lisp's string replace power.
  19600.  
  19601.  
  19602. Tcl
  19603. ---
  19604. Everything in Tcl is a string. It is very easy to map certain
  19605. string-intensive applications to Tcl. For example, I wrote a program to
  19606. do parallel library database searches in Tcl in about two weeks. Ranking
  19607. the 4 languages in terms of how easy it would have been: Tcl, Python,
  19608. Emacs, and I cant say about Perl because I gave up on Perl quickly after
  19609. buying the book and seeing all those registers and the confusing
  19610. context-intensive syntax. 
  19611.  
  19612.  
  19613. 
  19614. File: toolkit.languages.info  Node: Process Control, Prev: String Handling, Up: Top, Next: Extensions
  19615.  
  19616. Process Control
  19617. ===============
  19618.  
  19619.  
  19620. Emacs Lisp
  19621. ----------
  19622. Has specialized modes for controlling Common and other lisps to
  19623. facilitate debugging, execution, and programming. Has shell modes with
  19624. command history. Has an excellent ftp program (ange-ftp). Can
  19625. asynchronously or synchronously call the shell and store the output in a
  19626. buffer or string. Can filter output from a process. However, the
  19627. filtering is somewhat hairy because you cannot be sure of the packet
  19628. size that the data will arrive in. In other words, it is easy to open a
  19629. pipe with Perl/Python/Tcl and just read lines at a time but you have to
  19630. write an accumulator function to do this in Emacs Lisp. However, the
  19631. interactor mode for GNU Smalltalk has an excellent accumulator function
  19632. that you could use in your own code. 
  19633.  
  19634.  
  19635. Perl
  19636. ----
  19637. Chat2.pl .. I dont know
  19638.  
  19639.  
  19640. Python
  19641. ------
  19642. You would use pipes to do this in Python. It would be as easy as Emacs,
  19643. however, so much is already written for Emacs. Python is a young
  19644. language with far less supporters.
  19645.  
  19646.  
  19647. Tcl
  19648. ---
  19649. Even stronger than Emacs. A package by Don Libes called Expect allows
  19650. the programmer to specify a set of expected regexps from the process and
  19651. what to do upon the receipt of the process output. Several of his papers
  19652. including Expect: Programs for Controlling Interactive Processes are
  19653. available in postscript format for anonymous ftp from durer.cme.nist.giv
  19654. in pub/expect. 
  19655.  
  19656. 
  19657. File: toolkit.languages.info  Node: Extensions, Prev: Process Control, Up: Top, Next: Documentation
  19658.  
  19659. Extensions
  19660. ==========
  19661.  
  19662. An important feature of these languages is how much is already there for
  19663. you to use.
  19664.  
  19665.  
  19666. Emacs Lisp
  19667. ----------
  19668. Emacs is light years ahead of any of these other languages in this respect.
  19669.   1. 4 mail readers (rmail, vm, mh-e, elm interface)
  19670.   2. 2 Usenet news readers (gnus, rnews+)
  19671.   3. Ange-FTP -- An excellent ftp utility which allows transparent
  19672.      file access/modification with the ease of a few keystrokes.
  19673.   4. Countless interfaces for everything from Archie to MUD.
  19674.   5. AUC-TeX allows you to do anything you would want to do with
  19675.      TeX/LaTeX from an Emacs buffer quickly and easily.
  19676.   6. Calc is a poor man's Mathematica
  19677.   7. VIP -- vi emulation for emacs
  19678.   8. Calendar/Diary -- calendar manager within Emacs
  19679.   9. Tree Dired -- better directory editor for Emacs
  19680.  10. Hyperbole -- extensible hypertext management system within Emacs
  19681.  11. Ispell -- spell checker in C with interface for Emacs
  19682.  12. Patch -- program to apply "diffs" for updating files
  19683.  
  19684. Emacs' use of switchable modes allows for easy creation of new packages
  19685. with their own set of keyboard macros or they can simply use a
  19686. predefined mode.
  19687.  
  19688.  
  19689. Perl
  19690. ----
  19691.   1. chat2pl --
  19692.  
  19693.  
  19694. Python
  19695. ------
  19696.   1. ?? -- Multimedia interface. 
  19697.   2. ?? -- Remote Procedure Call debugger
  19698.   3. texfix -- Crude convert latex to texinfo
  19699.   4. throughput -- measure tcp throughput
  19700.   5. dutree -- format du(1) output at a tree sorted by size
  19701.   6. findlinks -- recursively find links to a given path prefix
  19702.   7. lpwatch -- watch BSD line printer queues
  19703.   8. suff -- sort a list of files by suffix 
  19704.  
  19705.  
  19706. Tcl
  19707. ===
  19708.   1. Basic Object System -- adds object oriented capabilities to Tcl. It is
  19709.      not class-based but prototype-based whatever that means.
  19710.   2. CD Rom Interface
  19711.   3. MXEdit -- Text editor
  19712.   4. tclTCP/rawTCP -- tcp/ip for tcl remote procedure calls and connect
  19713.      activity 
  19714.   5. VOGLE -- awesome full-color 3-D rendering package with a ton of
  19715.      fonts, both american and foreign.
  19716.  
  19717. * Menu:
  19718.  
  19719.  
  19720. * Documentation::
  19721. * Top::
  19722.  
  19723.  
  19724. 
  19725. File: toolkit.languages.info  Node: Documentation, Prev: Extensions, Up: Top, Next: Ease of Use
  19726.  
  19727. Documentation
  19728. *************
  19729.  
  19730.  
  19731. Emacs Lisp
  19732. ==========
  19733. Excellent. Both for using Emacs and programming Emacs Lisp.
  19734.  
  19735.  
  19736. Perl
  19737. ====
  19738. Excellent. A book is out by Larry Wall called Programming in Perl. The
  19739. 50-page man page is not bad.
  19740.  
  19741.  
  19742. Python
  19743. ======
  19744. Good. Covers everything but how to extend Python through C.
  19745.  
  19746.  
  19747. Tcl
  19748. ===
  19749. Good. The author of Tcl, John Ousterhout, will mail you the manual
  19750. pages. 
  19751.  
  19752. 
  19753. File: toolkit.languages.info  Node: Ease of Use, Prev: Documentation, Up: Top, Next: Extensibility
  19754.  
  19755. Ease of Use
  19756. ***********
  19757.  
  19758. Emacs Lisp
  19759. ==========
  19760. The best. Has a debugger. Since you are in Emacs, you can immediately
  19761. test whatever you are writing. Documentation on every function and
  19762. variable in memory is available in 2 keypresses. The tags system allows
  19763. you to jump to functions and global variable declarations without
  19764. knowing which file they are in. 
  19765.  
  19766. Perl
  19767. ====
  19768. Ok. There is a Perl mode for Emacs.
  19769.  
  19770. Python
  19771. ======
  19772. Good. You can send a buffer to a python interpreter by using Emacs.
  19773.  
  19774. Tcl
  19775. ===
  19776. Ok. The language itself is straightforward. There is a tcl mode for
  19777. Emacs but nothing written to
  19778. allow you to send a buffer of code to the tcl interpreter. Development
  19779. is still pretty fast.
  19780.  
  19781. 
  19782. File: toolkit.languages.info  Node: Extensibility, Prev: Ease of Use, Up: Top, Next: Test Suite
  19783.  
  19784. Extensibility
  19785. *************
  19786.  
  19787.  
  19788. Emacs Lisp
  19789. ==========
  19790. Writing new lisp functions is only feasible by using the
  19791. already-available lisp functions. To write new lisp functions in C would mean
  19792. re-compiling Emacs every time. 
  19793.  
  19794.  
  19795. Perl
  19796. ====
  19797. Very difficult.
  19798.  
  19799.  
  19800. Python
  19801. ======
  19802. Its possible in Python and C. How to do it in C is not well documented.
  19803.  
  19804.  
  19805. Tcl
  19806. ===
  19807. Its possible in Tcl and well-docuemented and has been down well by many
  19808. people in C.
  19809.  
  19810. 
  19811. File: toolkit.languages.info  Node: Test Suite, Prev: Top, Up: Top, Next: Top
  19812.  
  19813. Test Suite
  19814. **********
  19815.  
  19816.  
  19817. Emacs Lisp
  19818. ==========
  19819. Well, if you compile Emacs then Emacs Lisp will run. This is
  19820. irrelevant criteria for Emacs Lisp.
  19821.  
  19822. Perl
  19823. ====
  19824. Assorted programs here and there.
  19825.  
  19826. Python
  19827. ======
  19828. Exhaustive test suite
  19829.  
  19830. Tcl
  19831. ===
  19832. Exhaustive test suite
  19833.  
  19834.  
  19835. 
  19836. 
  19837. Received: from mail.unet.umn.edu by charon.cwi.nl with SMTP
  19838.     id AA12286 (5.65b/2.10/CWI-Amsterdam); Fri, 3 Jul 1992 22:15:10 +0200
  19839. Received: from s6.math.umn.edu by mail.unet.umn.edu (5.65c/)
  19840.     id AA05943; Fri, 3 Jul 1992 15:15:06 -0500
  19841. Date: Fri, 3 Jul 92 15:16:38 CDT
  19842. From: "Erik E. Rantapaa" <rantapaa@s5.math.umn.edu>
  19843. Message-Id: <9207032016.AA07260@s6.math.umn.edu>
  19844. Received: by s6.math.umn.edu; Fri, 3 Jul 92 15:16:38 CDT
  19845. To: python-list@cwi.nl
  19846. Subject: Survey of Emacs Lisp, Perl, Python, and Tcl (and Icon?)
  19847.  
  19848.  
  19849. brannon@jove.cs.caltech.edu (Terrence M. Brannon) writes:
  19850.  
  19851. >The purpose of this FAQ is to expose people to several languages which
  19852. >can cutdown/eliminate shell programming and C programming.
  19853.  
  19854. I would suggest adding Icon to your survey.  It runs on most Unix platforms,
  19855. can be interpreted or compiled, and has been used for a lot of the things
  19856. that shells scripts and Perl has been used for. 
  19857.  
  19858. Unforunately I am not that experienced with using it so I can't give
  19859. you more particulars -- perhaps someone else can (or ask comp.lang.icon).
  19860. 
  19861. 
  19862. Replied: Sun, 05 Jul 1992 12:48:16 +0200
  19863. Replied: "brannon@jove.cs.caltech.edu (Terrence M. Brannon) "
  19864. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  19865.     id AA23413 (5.65b/2.10/CWI-Amsterdam); Sat, 4 Jul 1992 04:16:32 +0200
  19866. Received: from stun4r.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  19867.     id AA01246; Fri, 3 Jul 92 19:16:12 PDT
  19868. Date: Fri, 3 Jul 92 19:16:12 PDT
  19869. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  19870. Message-Id: <9207040216.AA01246@jove.cs.caltech.edu>
  19871. Received: by stun4r.cs.caltech.edu (4.1/SMI-4.1)
  19872.     id AA04421; Fri, 3 Jul 92 19:16:28 PDT
  19873. To: python-list@cwi.nl
  19874. Subject: 2 inter-related questions
  19875. Comments: Hyperbole mail buttons accepted, v3.02P.
  19876.  
  19877. ( 1 )
  19878.  
  19879. Ok, since my python script requires argv[1], I have to call it from
  19880. the shell, right ? There is not way from the python interpreter to go:
  19881.  
  19882. >>> set shellProgramArgs = 'test2.c'
  19883. >>> exec 'compilify'
  19884.  
  19885. So that Python will issue compilify test2.c to the shell right ?
  19886.  
  19887.  
  19888. ( 2 )
  19889.  
  19890. After I run a script from the shell , I sometimes would like to drop
  19891. into the interpreter to see the values of some things . Is there a
  19892. Python command to do this ?
  19893.  
  19894.  
  19895.  
  19896. See so basically what has happened is I cant use the python shell to
  19897. test values after running a program and I also cant let a script
  19898. running frm csh call the python shell after it has finished so I can
  19899. examine values.
  19900.  
  19901. ( 3 )
  19902.  
  19903. os.popen execs its argument based on its own shell , not the one with
  19904. my PATH environmental variable . As a result , it cant find things
  19905. that are own my path that are not on the default PATH . Besides moving
  19906. the executable to the current directory or another place on the
  19907. default PATH, is there a workaround ?
  19908.  
  19909.  
  19910. Also : all of this is going into a FAQ sheet .
  19911.  
  19912.  
  19913. --
  19914. terrence brannon
  19915. brannon@jove.cs.caltech.edu 
  19916.  
  19917. 
  19918. 
  19919. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  19920.     id AA14644 (5.65b/2.10/CWI-Amsterdam); Sun, 5 Jul 1992 12:49:18 +0200
  19921. Received: by voorn.cwi.nl with SMTP; Sun, 5 Jul 1992 10:49:21 GMT
  19922. Message-Id: <9207051049.AA12772@voorn.cwi.nl>
  19923. To: python-list@cwi.nl
  19924. Subject: Re: 2 inter-related questions 
  19925. In-Reply-To: Your message of "Fri, 03 Jul 1992 19:16:12 MDT."
  19926.              <9207040216.AA01246@jove.cs.caltech.edu> 
  19927. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  19928. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  19929. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  19930. Date: Sun, 05 Jul 1992 12:49:20 +0200
  19931. Sender: Guido.van.Rossum@cwi.nl
  19932.  
  19933. >Ok, since my python script requires argv[1], I have to call it from
  19934. >the shell, right ? There is not way from the python interpreter to go:
  19935. >
  19936. >>>> set shellProgramArgs = 'test2.c'
  19937. >>>> exec 'compilify'
  19938. >
  19939. >So that Python will issue compilify test2.c to the shell right ?
  19940.  
  19941. I have no idea what you want here -- it's totally un-Python syntax and
  19942. I don't know what 'compilify' is supposed to mean, but:
  19943.  
  19944. (a) a Python receives its arguments in sys.argv; you can interactively
  19945. import sys and assign anything you want to sys.argv.
  19946.  
  19947. (b) The 'os module defines an exec() function with the same
  19948. functionality as exec(2) in the C library.
  19949.  
  19950. (c) To run a Python script from an interactive Python interpreter, you
  19951. can either use the built-in function evalfile(), or (if the script's
  19952. file name ends in '.py') import it as a module.  The first time you
  19953. import a module its statements are executed -- this initializes the
  19954. module.  To re-run the statements in an imported module, use the
  19955. built-in function reload().
  19956.  
  19957. >( 2 )
  19958. >
  19959. >After I run a script from the shell , I sometimes would like to drop
  19960. >into the interpreter to see the values of some things . Is there a
  19961. >Python command to do this ?
  19962.  
  19963. No, but you can run the script from an interactive Python interpreter,
  19964. as explained above.
  19965.  
  19966. >See so basically what has happened is I cant use the python shell to
  19967. >test values after running a program and I also cant let a script
  19968. >running frm csh call the python shell after it has finished so I can
  19969. >examine values.
  19970.  
  19971. Again I don't know how to interpret your sentence, but maybe I've
  19972. answered it anyway?
  19973.  
  19974. >( 3 )
  19975. >
  19976. >os.popen execs its argument based on its own shell , not the one with
  19977. >my PATH environmental variable . As a result , it cant find things
  19978. >that are own my path that are not on the default PATH . Besides moving
  19979. >the executable to the current directory or another place on the
  19980. >default PATH, is there a workaround ?
  19981.  
  19982. Sounds like a problem in your C library -- on my system, popen
  19983. definitely sees my $PATH.  Are you sure you are exporting $PATH from
  19984. your shell?
  19985.  
  19986. >Also : all of this is going into a FAQ sheet .
  19987.  
  19988. Is this a threat? :-)
  19989.  
  19990. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  19991. "Kemal Ataturk had an entire menagerie called Abdul."
  19992. 
  19993. 
  19994. Received: from meermin.cwi.nl by charon.cwi.nl with SMTP
  19995.     id AA00230 (5.65b/2.10/CWI-Amsterdam); Sun, 5 Jul 1992 20:58:40 +0200
  19996. Received: from hopscotch.ksr.com by meermin.cwi.nl with SMTP
  19997.     id AA12345 (5.65a/2.10/CWI-Amsterdam); Sun, 5 Jul 1992 18:20:08 GMT
  19998. Received: from ksr.com (frankenstein.ksr.com) by hopscotch.ksr.com with SMTP
  19999.     id AA02093; Sun, 5 Jul 1992 14:18:09 -0400
  20000. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  20001.     id AA28895; Sun, 5 Jul 92 14:19:03 EDT
  20002. Received: by kaos.ksr.com (4.0/KSR-2.0)
  20003.     id AA16047; Sun, 5 Jul 92 14:19:02 EDT
  20004. Message-Id: <9207051819.AA16047@kaos.ksr.com>
  20005. To: python-list@cwi.nl
  20006. Subject: Re: 2 inter-related questions
  20007. Date: Sun, 05 Jul 92 14:19:02 -0400
  20008. From: Tim Peters <tim@ksr.com>
  20009.  
  20010. >>Ok, since my python script requires argv[1], I have to call it from
  20011. >>the shell, right ? There is not way from the python interpreter to go:
  20012. >>
  20013. >>>>> set shellProgramArgs = 'test2.c'
  20014. >>>> exec 'compilify'
  20015. >>
  20016. >>So that Python will issue compilify test2.c to the shell right ?
  20017.  
  20018. I'm not sure what you're after here either, but suspect the following
  20019. might do the trick.  Here's file "compilify":
  20020.  
  20021. #!/lang/users/tim/bin/python
  20022.  
  20023. import sys
  20024. print 'I think my name is', sys.argv[0]
  20025. print 'I think my arguments are', sys.argv[1:]
  20026.  
  20027. And here's an interpreter session:
  20028.  
  20029. >>> import os
  20030. >>> os.system('compilify test2.c')
  20031. I think my name is ./compilify
  20032. I think my arguments are ['test2.c']
  20033. 0
  20034. >>>
  20035.  
  20036. Note that "0" was printed by the interpreter; it's the exit status of
  20037. the "system" call.  You can suppress that by assigning to a dummy vrbl,
  20038. like:
  20039.  
  20040. >>> dummy = os.system('compilify test2.c')
  20041. I think my name is ./compilify
  20042. I think my arguments are ['test2.c']
  20043. >>>
  20044.  
  20045. I doubt you really want the `os.exec' function, because that will
  20046. replace the interpreter with the thing being exec'ed.
  20047.  
  20048. As Guido mentioned, this will also do the trick (so long as the thing
  20049. you want to run from the interpreter is a Python script; 'system' and
  20050. 'exec' can execute anything):
  20051.  
  20052. >>> import sys
  20053. >>> sys.argv = ['blah', 'test2.c']
  20054. >>> execfile('compilify')
  20055. I think my name is blah
  20056. I think my arguments are ['test2.c']
  20057. >>>
  20058.  
  20059. >>os.popen execs its argument based on its own shell , not the one with
  20060. >>my PATH environmental variable . As a result , it cant find things
  20061. >>that are own my path that are not on the default PATH . Besides moving
  20062. >>the executable to the current directory or another place on the
  20063. >>default PATH, is there a workaround ?
  20064.  
  20065. >[guido]
  20066. >Sounds like a problem in your C library -- on my system, popen
  20067. >definitely sees my $PATH.  Are you sure you are exporting $PATH from
  20068. >your shell?
  20069.  
  20070. I don't think I understood the original question -- an example would
  20071. sure help!
  20072.  
  20073. But no problems with $PATH here either.  I'll just add that if the thing
  20074. being popen'd is a Python script, it won't find $PATH in sys.path, but
  20075. in os.environ['PATH'].  I've seen Python beginners panic over that
  20076. before (they look at sys.path & figure their $PATH has been destroyed).
  20077.  
  20078. >>Also : all of this is going into a FAQ sheet .
  20079. >Is this a threat? :-)
  20080.  
  20081. no-but-this-is<grin>-ly y'rs  - tim
  20082.  
  20083. Tim Peters   Kendall Square Research Corp
  20084. tim@ksr.com,         ksr!tim@uunet.uu.net
  20085. 
  20086. 
  20087. Received: from ansjovis.cwi.nl by charon.cwi.nl with SMTP
  20088.     id AA18910 (5.65b/2.10/CWI-Amsterdam); Mon, 6 Jul 1992 10:45:07 +0200
  20089. Received: by ansjovis.cwi.nl with SMTP; Mon, 6 Jul 1992 08:45:06 GMT
  20090. Message-Id: <9207060845.AA17998@ansjovis.cwi.nl>
  20091. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  20092. Cc: python-list@cwi.nl
  20093. Subject: Re: Survey of Emacs Lisp, Perl, Python, and Tcl 
  20094. In-Reply-To: Your message of Fri, 03 Jul 1992 10:11:25 -0700.
  20095.              <9207031711.AA00981@jove.cs.caltech.edu> 
  20096. Date: Mon, 06 Jul 1992 10:45:05 +0200
  20097. From: Sjoerd Mullender <Sjoerd.Mullender@cwi.nl>
  20098.  
  20099. On Fri, Jul 3 1992 Terrence M. Brannon wrote:
  20100.  
  20101. > Python
  20102. > ======
  20103. > An object-oriented interpreted language. Callable from C as well as the
  20104. > shell. Interpretation may be sidestepped through the use of the
  20105. > byte-compiler on files. Python played a major role in testing Amoeba, a
  20106. > distributed operating system developed at CWI.
  20107.  
  20108. Amoeba existed long before Python.  It is true that Python was ported
  20109. to Amoeba (i.e. ran under Amoeba) and that the Amoeba primitives were
  20110. ported to Python, but is can hardly be said that "Python played a
  20111. major role in testing Amoeba."
  20112. Also, Amoeba was a joint project of the Free University and CWI, both
  20113. in Amsterdam.  The project originated at the Free University, and
  20114. since CWI stopped the project a while ago, it is once again a project
  20115. of the Free University alone.
  20116.  
  20117. > Python
  20118. > ------
  20119. > Through the use of the STDWIN paradigm, the same source code
  20120. > can do graphics in the following systems:
  20121. >   1. X-windows
  20122. >   2. Macintosh
  20123. >   3. Atari ST
  20124. >   4. DOS
  20125. >   5. Silicon Graphics SGI workstations
  20126. > This approach allows flexibility but means that no one graphics system's
  20127. > potential is maximized. 
  20128.  
  20129. On the Silicon Graphics, there is also an interface availible to GL,
  20130. the grpahics library, and to FORMS, a graphical user interface toolkit
  20131. for Silicon Graphics workstations by Mark H. Overmars.
  20132.  
  20133. Sjoerd Mullender
  20134. CWI, dept. CST, Kruislaan 413, 1098 SJ Amsterdam, Netherlands
  20135. email: Sjoerd.Mullender@cwi.nl        fax:   +31 20 592 4199
  20136. phone: +31 20 592 4127            telex: 12571 mactr nl
  20137. 
  20138. 
  20139. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  20140.     id AA25566 (5.65b/2.10/CWI-Amsterdam); Mon, 6 Jul 1992 16:15:02 +0200
  20141. Received: by schelvis.cwi.nl with SMTP; Mon, 6 Jul 1992 14:14:58 GMT
  20142. Message-Id: <9207061414.AA01092@schelvis.cwi.nl>
  20143. To: python-list@cwi.nl
  20144. Subject: Python and audio
  20145. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  20146. Phone: +31 20 5924098(work), +31 20 6160335(home)
  20147. X-Last-Band-Seen: Kevin Coyne (Vondelpark, 28-6)
  20148. X-Mini-Review: Good! He does not look too healthy in the sun, though...
  20149. Date: Mon, 06 Jul 1992 16:14:57 +0200
  20150. From: Jack Jansen <Jack.Jansen@cwi.nl>
  20151.  
  20152. Is anyone else interested in using python for audio programming? I've
  20153. written a couple of extension modules to handle the sun audio device
  20154. (the SGI audio library was already supported) and to do all sorts of
  20155. operations on audio like conversion from/to U-LAW, ADPCM, etc. that
  20156. I'd be willing to share...
  20157. --
  20158. Jack Jansen        | If I can't dance I don't want to be part of
  20159. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  20160. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  20161. 
  20162. 
  20163. Replied: Tue, 07 Jul 1992 10:11:06 +0200
  20164. Replied: "louk@research.teleride.on.ca (Lou Kates) "
  20165. Received: from uunet.ca by charon.cwi.nl with SMTP
  20166.     id AA08661 (5.65b/2.10/CWI-Amsterdam); Tue, 7 Jul 1992 00:20:17 +0200
  20167. Received: from tslwat by mail.uunet.ca with UUCP id <10200>; Mon, 6 Jul 1992 18:20:01 -0400
  20168. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  20169.     id <m0m51Ii-0001wPC@research.teleride.on.ca>; Mon, 6 Jul 92 18:12 EDT
  20170. Message-Id: <m0m51Ii-0001wPC@research.teleride.on.ca>
  20171. Date:     Mon, 6 Jul 1992 18:12:00 -0400
  20172. From: louk@research.teleride.on.ca (Lou Kates)
  20173. To: python-list@cwi.nl
  20174. Subject: quickie filters
  20175.  
  20176.  
  20177. In perl there are flags to:
  20178.  
  20179.      a.   read the program from the command line
  20180.  
  20181.      b.   iterate over all the arguments using them as successive
  20182.       input files
  20183.  
  20184.      c.   automatically split each input line
  20185.  
  20186. This makes it easy to write quickie filters. 
  20187.  
  20188. Perhaps this might be made available for python too? For example,
  20189. suppose that -a is autosplit mode and -e reads the python program
  20190. from the  next argument.  Then one  might do the following to get
  20191. the second  field  from each line  and its  square assuming  that
  20192. Python puts   the line into L  and  then splits it into a list of
  20193. fields F:
  20194.  
  20195.     python -ae 'print F[1], F[1]*F[1]' 
  20196.  
  20197. if you want to read the input from standard input, or
  20198.  
  20199.     python -ae 'print F[1], F[1]*F[1]' infile.dat
  20200.  
  20201. if you want to name the file explicity.
  20202.  
  20203. ---
  20204. Lou Kates, louk@research.teleride.on.ca
  20205. 
  20206. 
  20207. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  20208.     id AA23939 (5.65b/2.10/CWI-Amsterdam); Tue, 7 Jul 1992 10:11:44 +0200
  20209. Received: by voorn.cwi.nl with SMTP; Tue, 7 Jul 1992 08:11:49 GMT
  20210. Message-Id: <9207070811.AA16269@voorn.cwi.nl>
  20211. To: python-list@cwi.nl
  20212. Subject: Re: quickie filters 
  20213. In-Reply-To: Your message of "Mon, 06 Jul 1992 18:12:00 MDT."
  20214.              <m0m51Ii-0001wPC@research.teleride.on.ca> 
  20215. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20216. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  20217. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  20218. Date: Tue, 07 Jul 1992 10:11:48 +0200
  20219. Sender: Guido.van.Rossum@cwi.nl
  20220.  
  20221. >In perl there are flags to:
  20222. >
  20223. >     a.   read the program from the command line
  20224.  
  20225. Python has this too -- it's called '-c commands' in analogy with *sh.
  20226.  
  20227. >     b.   iterate over all the arguments using them as successive
  20228. >      input files
  20229. >
  20230. >     c.   automatically split each input line
  20231. >
  20232. >This makes it easy to write quickie filters.
  20233. [examples omitted]
  20234.  
  20235. Remember that this works with Perl because many other features of the
  20236. language are designed to cooperate, e.g. the defaults for regexp
  20237. matching etc.  Frankly, I'd rather not try to compete with Perl in the
  20238. areas where Perl is best -- it's a battle that's impossible to win,
  20239. and I don't think it is a good idea to strive for the number of
  20240. obscure options and shortcuts that Perl has acquired through the year.
  20241. If you want Perl, use it -- Python wins where you need sophisticated
  20242. data structures or object-orientation.
  20243.  
  20244. On the other hand, it isn't hard to write a Python script that
  20245. emulates these options.  Here's a simple one that emulates -ae:
  20246.  
  20247. ------------------------------------------------------------------------
  20248. #! /usr/local/python
  20249.  
  20250. # Wrapper around Python to emulate the Perl -ae options:
  20251. # (1) first argument is a Python command
  20252. # (2) rest of arguments are input files (default or '-': read stdin)
  20253. # (3) each line is put into the string L with trailing '\n' stripped
  20254. # (4) the fields of the line are put in the list F
  20255. # (5) also: FILE: full filename; LINE: full line; FP: open file object
  20256.  
  20257. import sys
  20258. import string
  20259.  
  20260. command = sys.argv[1]
  20261.  
  20262. if not sys.argv[2:]:
  20263.     sys.argv[2:] = ['-']
  20264.  
  20265. for FILE in sys.argv[2:]:
  20266.     if FILE == '-':
  20267.         FP = sys.stdin
  20268.     else:
  20269.         FP = open(FILE, 'r')
  20270.     while 1:
  20271.         LINE = FP.readline()
  20272.         if not LINE: break
  20273.         L = LINE[:-1]
  20274.         F = string.split(L)
  20275.         exec(command)
  20276. ------------------------------------------------------------------------
  20277.  
  20278. This version is inefficient because it parses and "compiles" the
  20279. command for each input line, but it is possible to solve that by
  20280. creative use of exec().  You can also imagine more useful handling of
  20281. missing input files, syntax errors in the script, etc.
  20282.  
  20283. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  20284. "Well I'm a plumber.  I can't act"
  20285. 
  20286. 
  20287. Received: from uu.psi.com by charon.cwi.nl with SMTP
  20288.     id AA05049 (5.65b/2.10/CWI-Amsterdam); Tue, 7 Jul 1992 18:55:06 +0200
  20289. Received: from midas.UUCP by uu.psi.com (5.65b/4.1.031792-PSI/PSINet)
  20290.     id AA20632; Tue, 7 Jul 92 12:38:10 -0400
  20291. Received: from lynchpin.mtkgc.com by mtkgc.COM (4.1/SMI-4.1)
  20292.     id AA03203; Tue, 7 Jul 92 08:53:46 EDT
  20293. Received: by lynchpin.mtkgc.com (4.1/SMI-4.1)
  20294.     id AA13532; Tue, 7 Jul 92 08:55:11 EDT
  20295. Date: Tue, 7 Jul 92 08:55:11 EDT
  20296. From: samborn@lynchpin.mtkgc.COM (Kevin Samborn)
  20297. Message-Id: <9207071255.AA13532@lynchpin.mtkgc.com>
  20298. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20299. Subject: Re: what is the 256 on my screen ? 
  20300. References: <9207022017.AA28196@jove.cs.caltech.edu>
  20301.     <9207022048.AA08441@voorn.cwi.nl>
  20302.  
  20303. /\/\ On Thu, 02 Jul 1992 22:48:56 +0200, Guido van Rossum said:
  20304.  
  20305.    Terrence> i am starting up a faq for python by the way
  20306.  
  20307.    Guido> That's great.  Will it be ftp'able?  I might point new
  20308.    Guido> readers of the list to it, to get them up at full speed
  20309.    Guido> quickly...
  20310.  
  20311. Guido- you might consider posting it periodically to news.answers.
  20312. This will make the list easily available (and automatically archived),
  20313. and expose lots and lots of people to Python...
  20314.  
  20315.  
  20316. kevin
  20317.  
  20318. --
  20319. kevin samborn
  20320. samborn@mtkgc.com
  20321.  
  20322. 
  20323. 
  20324. Replied: Wed, 08 Jul 1992 10:53:48 +0200
  20325. Replied: "python-list@cwi.nl "
  20326. Received: from uunet.ca by charon.cwi.nl with SMTP
  20327.     id AA19800 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Jul 1992 02:14:11 +0200
  20328. Received: from tslwat by mail.uunet.ca with UUCP id <10219>; Tue, 7 Jul 1992 20:14:05 -0400
  20329. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  20330.     id <m0m5OoG-0001VmC@research.teleride.on.ca>; Tue, 7 Jul 92 19:19 EDT
  20331. Message-Id: <m0m5OoG-0001VmC@research.teleride.on.ca>
  20332. From: louk@RESEARCH.TELERIDE.ON.CA (Lou Kates)
  20333. Subject: Re: quickie filters
  20334. To: python-list@cwi.nl
  20335. Date:     Tue, 7 Jul 1992 19:18:59 -0400
  20336. X-Mailer: ELM [version 2.3 PL11]
  20337.  
  20338. > >In perl there are flags to:
  20339. > >     a.   read the program from the command line
  20340. > >     b.   iterate over all the arguments using them as successive
  20341. > >         input files
  20342. > >     c.   automatically split each input line
  20343. > >
  20344. > >This makes it easy to write quickie filters.
  20345. ...
  20346. > On the other hand, it isn't hard to write a Python script that
  20347. > emulates these options.  Here's a simple one that emulates -ae:
  20348. [example omitted]
  20349.  
  20350. This example  seems sufficiently useful,  in general, that it (or
  20351. possibly a sligthly enhanced  version of  it) should be added  to
  20352. the standard  distributed  Python library. It is  useful not only
  20353. for itself but also as a learning example of how you do this sort
  20354. of thing in Python.
  20355.  
  20356. Lou Kates, louk@research.teleride.on.ca
  20357. 
  20358. 
  20359. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  20360.     id AA27699 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Jul 1992 10:53:48 +0200
  20361. Received: by voorn.cwi.nl with SMTP; Wed, 8 Jul 1992 08:53:51 GMT
  20362. Message-Id: <9207080853.AA18957@voorn.cwi.nl>
  20363. To: python-list@cwi.nl
  20364. Subject: Re: quickie filters 
  20365. In-Reply-To: Your message of "Tue, 07 Jul 1992 19:18:59 MDT."
  20366.              <m0m5OoG-0001VmC@research.teleride.on.ca> 
  20367. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20368. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  20369. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  20370. Date: Wed, 08 Jul 1992 10:53:50 +0200
  20371. Sender: Guido.van.Rossum@cwi.nl
  20372.  
  20373. Lou Kates writes:
  20374. >This example  seems sufficiently useful,  in general, that it (or
  20375. >possibly a sligthly enhanced  version of  it) should be added  to
  20376. >the standard  distributed  Python library. It is  useful not only
  20377. >for itself but also as a learning example of how you do this sort
  20378. >of thing in Python.
  20379.  
  20380. We are in almost complete agreement!  Here's a (much enhanced) version
  20381. that supports more Perl options and parses the script only once.  I
  20382. added the -F option in analogy to awk; how do you set the field
  20383. separator in Perl?
  20384.  
  20385. I've called it "pp.py" and (some version of) it will appear under this
  20386. name as a demo script in the next release (whenever that is).  Adding
  20387. it to the library won't do much good since the Python library consists
  20388. of modules that you can import from other Python programs, while this
  20389. is a "script" that normally runs as a main program.
  20390.  
  20391. Nasty bug warning: calling it with the option string "-na" will cause
  20392. weird behavior if your Python interpreter has stdwin compiled in :-(.
  20393. This is because the X11 option processing will eat the "-na" (which it
  20394. thinks is an abbreviation for "-name") and the following argument.
  20395. Using "-n -a" will work fine.
  20396.  
  20397. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  20398. "Now, let's get one thing *quite* clear.  I most definitely told you!"
  20399.  
  20400. ----------------------------- pp.py ------------------------------------
  20401. #! /usr/local/python
  20402.  
  20403. # Emulate some Perl command line options.
  20404. # Usage: pp [-a] [-c] [-e scriptline] [-F fieldsep] [-n] [-p] [file] ...
  20405. # Where the options mean the following:
  20406. #   -a            : together with -n or -p, splits each line into list F
  20407. #   -c            : check syntax only, do not execute any code
  20408. #   -e scriptline : gives one line of the Python script; may be repeated
  20409. #   -F fieldsep   : sets the field separator for the -a option [not in Perl]
  20410. #   -n            : runs the script for each line of input
  20411. #   -p            : prints the line after the script has run
  20412. # When no script lines have been passed, the first file argument
  20413. # contains the script.  With -n or -p, the remaining arguments are
  20414. # read as input to the script, line by line.  If a file is '-'
  20415. # or missing, standard input is read.
  20416.  
  20417. import sys
  20418. import string
  20419. import getopt
  20420.  
  20421. FS = ''
  20422. SCRIPT = []
  20423. AFLAG = 0
  20424. CFLAG = 0
  20425. NFLAG = 0
  20426. PFLAG = 0
  20427.  
  20428. try:
  20429.     optlist, ARGS = getopt.getopt(sys.argv[1:], 'ace:F:np')
  20430. except getopt.error, msg:
  20431.     sys.stderr.write(sys.argv[0] + ': ' + msg + '\n')
  20432.     sys.exit(2)
  20433.  
  20434. for option, optarg in optlist:
  20435.     if option == '-a':
  20436.         AFLAG = 1
  20437.     elif option == '-c':
  20438.         CFLAG = 1
  20439.     elif option == '-e':
  20440.         for line in string.splitfields(optarg, '\n'):
  20441.             SCRIPT.append(line)
  20442.     elif option == '-F':
  20443.         FS = optarg
  20444.     elif option == '-n':
  20445.         NFLAG = 1
  20446.         PFLAG = 0
  20447.     elif option == '-p':
  20448.         NFLAG = 1
  20449.         PFLAG = 1
  20450.     else:
  20451.         print option, 'not recognized???'
  20452.  
  20453. if not ARGS: ARGS.append('-')
  20454.  
  20455. if not SCRIPT:
  20456.     if ARGS[0] == '-':
  20457.         fp = sys.stdin
  20458.     else:
  20459.         fp = open(ARGS[0], 'r')
  20460.     while 1:
  20461.         line = fp.readline()
  20462.         if not line: break
  20463.         SCRIPT.append(line[:-1])
  20464.     del fp
  20465.     del ARGS[0]
  20466.     if not ARGS: ARGS.append('-')
  20467.  
  20468. if CFLAG:
  20469.     prologue = ['if 0:']
  20470.     epilogue = []
  20471. elif NFLAG:
  20472.     # Note that it is on purpose that AFLAG and PFLAG are
  20473.     # tested dynamically each time through the loop
  20474.     prologue = [ \
  20475.         'LINECOUNT = 0', \
  20476.         'for FILE in ARGS:', \
  20477.         '   \tif FILE == \'-\':', \
  20478.         '   \t   \tFP = sys.stdin', \
  20479.         '   \telse:', \
  20480.         '   \t   \tFP = open(FILE, \'r\')', \
  20481.         '   \tLINENO = 0', \
  20482.         '   \twhile 1:', \
  20483.         '   \t   \tLINE = FP.readline()', \
  20484.         '   \t   \tif not LINE: break', \
  20485.         '   \t   \tLINENO = LINENO + 1', \
  20486.         '   \t   \tLINECOUNT = LINECOUNT + 1', \
  20487.         '   \t   \tL = LINE[:-1]', \
  20488.         '   \t   \taflag = AFLAG', \
  20489.         '   \t   \tif aflag:', \
  20490.         '   \t   \t   \tif FS: F = string.splitfields(L, FS)', \
  20491.         '   \t   \t   \telse: F = string.split(L)' \
  20492.         ]
  20493.     epilogue = [ \
  20494.         '   \t   \tif not PFLAG: continue', \
  20495.         '   \t   \tif aflag:', \
  20496.         '   \t   \t   \tif FS: print string.joinfields(F, FS)', \
  20497.         '   \t   \t   \telse: print string.join(F)', \
  20498.         '   \t   \telse: print L', \
  20499.         ]
  20500. else:
  20501.     prologue = ['if 1:']
  20502.     epilogue = []
  20503.  
  20504. # Note that we indent using tabs only, so that any indentation style
  20505. # used in 'command' will come out right after re-indentation.
  20506.  
  20507. program = string.joinfields(prologue, '\n') + '\n'
  20508. for line in SCRIPT:
  20509.     program = program + ('   \t   \t' + line + '\n')
  20510. program = program + (string.joinfields(epilogue, '\n') + '\n')
  20511.  
  20512. import tempfile
  20513. tfn = tempfile.mktemp()
  20514. try:
  20515.     fp = open(tfn, 'w')
  20516.     fp.write(program)
  20517.     fp.close()
  20518.     execfile(tfn)
  20519. finally:
  20520.     import os
  20521.     try:
  20522.         os.unlink(tfn)
  20523.     except:
  20524.         pass
  20525. 
  20526. 
  20527. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  20528.     id AA28341 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Jul 1992 11:36:41 +0200
  20529. Received: by schelvis.cwi.nl with SMTP; Wed, 8 Jul 1992 09:36:40 GMT
  20530. Message-Id: <9207080936.AA05010@schelvis.cwi.nl>
  20531. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20532. Cc: python-list@cwi.nl
  20533. Subject: Re: quickie filters 
  20534. In-Reply-To: Message by Guido van Rossum <Guido.van.Rossum@cwi.nl> ,
  20535.          Wed, 08 Jul 1992 10:53:50 +0200 , <9207080853.AA18957@voorn.cwi.nl> 
  20536. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  20537. Phone: +31 20 5924098(work), +31 20 6160335(home)
  20538. X-Last-Band-Seen: Scene (Vondelpark, 5-7)
  20539. X-Mini-Review: Too crowded.
  20540. Date: Wed, 08 Jul 1992 11:36:39 +0200
  20541. From: Jack Jansen <Jack.Jansen@cwi.nl>
  20542.  
  20543. Python is a truly wonderful language. When somebody comes up with a
  20544. good idea it takes about 1 minute and five lines to program something
  20545. that almost does what you want. Then it takes only an hour to extend
  20546. the script to 300 lines, after which it still does almost what you
  20547. want:-)
  20548. --
  20549. Jack Jansen        | If I can't dance I don't want to be part of
  20550. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  20551. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  20552. 
  20553. 
  20554. Received: from uunet.ca by charon.cwi.nl with SMTP
  20555.     id AA03106 (5.65b/2.10/CWI-Amsterdam); Wed, 8 Jul 1992 16:05:17 +0200
  20556. Received: from tslwat by mail.uunet.ca with UUCP id <9720>; Wed, 8 Jul 1992 10:04:54 -0400
  20557. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  20558.     id <m0m5bUd-0001efC@research.teleride.on.ca>; Wed, 8 Jul 92 08:51 EDT
  20559. Message-Id: <m0m5bUd-0001efC@research.teleride.on.ca>
  20560. From: louk@research.teleride.on.ca (Lou Kates)
  20561. Subject: Re: quickie filters 
  20562. To: python-list@cwi.nl
  20563. Date:     Wed, 8 Jul 1992 08:51:34 -0400
  20564. X-Mailer: ELM [version 2.3 PL11]
  20565.  
  20566.  
  20567. > We are in almost complete agreement!  Here's a (much enhanced) version
  20568. > that supports more Perl options and parses the script only once.  I
  20569.  
  20570. Thanks!
  20571.  
  20572. > added the -F option in analogy to awk; how do you set the field
  20573. > separator in Perl?
  20574.  
  20575. I don't think there is any way  to specify the field separator to
  20576. autosplit mode in  Perl like  you can in awk. If you do the split
  20577. yourself rather than use autosplit mode  then you can specify the
  20578. pattern on which to split as an argument to the split function. I
  20579. am not  a Perl expert but this is  my understanding from  reading
  20580. the manual I have.
  20581.  
  20582. Lou Kates, louk@research.teleride.on.ca
  20583. 
  20584. 
  20585. Replied: Thu, 09 Jul 1992 10:06:11 +0200
  20586. Replied: "Python Mailing List <python-list@cwi.nl> "
  20587. Received: from [138.95.19.12] by charon.cwi.nl with SMTP
  20588.     id AA18809 (5.65b/2.10/CWI-Amsterdam); Thu, 9 Jul 1992 01:30:49 +0200
  20589. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  20590.     id AA08017; Wed, 8 Jul 92 16:28:34 -0700
  20591. Received: by eng2.sequent.com (5.65/1.34)
  20592.     id AA03564; Wed, 8 Jul 92 16:30:35 -0700
  20593. Message-Id: <9207082330.AA03564@eng2.sequent.com>
  20594. To: Python Mailing List <python-list@cwi.nl>
  20595. Subject: python and stdwin: question and observation
  20596. Priority: Normal
  20597. Precedence: first-class
  20598. Organization: Sequent Computer Systems, Inc.
  20599.           Decision Support Group - MailStop: RHE2-358
  20600.           15450 S.W. Koll Parkway
  20601.           Beaverton, OR  97006
  20602. X-Phone: (503) 578-4404
  20603. X-Fax: (503) 578-7569
  20604. X-Uucp: ...!uunet!sequent!jaap
  20605. X-Internet: jaap@sequent.com
  20606. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  20607.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  20608.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  20609. Date: Wed, 08 Jul 92 16:30:34 PDT
  20610. From: Jaap Vermeulen <jaap@sequent.com>
  20611.  
  20612.  
  20613. I was looking around for a listbox inside a window.  I can't find any
  20614. example such a thing (maybe I'm just overlooking it?).  Mainly I want
  20615. to position two listboxes (or scrollboxes or whatever you wanna call
  20616. 'm) within a single window.
  20617.  
  20618. While I was looking around to find a listbox, I looked at the DirList
  20619. code and ran the TestDirList.py stdwin demo.  Now this demo is
  20620. *painfully* slow and uses gobs and gobs of processor time.  Is this
  20621. normal?
  20622.  
  20623. % time Python TestDirList.py
  20624.  
  20625. Time: 2:28.41 total  137.090 user  4.080 system  95.1% CPU
  20626. IO:   59+1 io  391+3 r/w syscalls  0+0 raw r/w
  20627. Mem:  0+14058 maj/min pf  137+344 incr/decr pff  0 max 187 zero 0 swaps
  20628. Sys:  147+1 vol/invol ctxsw  2306 system calls  0 signals
  20629.  
  20630. This means that in a little over two minutes I burned a little over two
  20631. minutes of CPU time (gulp!).  And the only thing I did is open five
  20632. directories or so (I started somewhere in ~/lib/python/demos/stdwin and
  20633. went up to the parent directory five times until I was in my home
  20634. directory).  And the code is not spinning, since it wouldn't burn
  20635. anything if I didn't touch it.  Insights anyone (read:  Guido :-).
  20636.  
  20637. Thanks,
  20638.  
  20639.     -Jaap-
  20640. --
  20641. Jaap Vermeulen                    +--------------------------+
  20642.                         | Sequent Computer Systems |
  20643.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  20644.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  20645. 
  20646. 
  20647. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  20648.     id AA04556 (5.65b/2.10/CWI-Amsterdam); Thu, 9 Jul 1992 10:06:13 +0200
  20649. Received: by voorn.cwi.nl with SMTP; Thu, 9 Jul 1992 08:06:15 GMT
  20650. Message-Id: <9207090806.AA21387@voorn.cwi.nl>
  20651. To: Python Mailing List <python-list@cwi.nl>
  20652. Subject: Re: python and stdwin: question and observation 
  20653. In-Reply-To: Your message of "Wed, 08 Jul 1992 16:30:34 MDT."
  20654.              <9207082330.AA03564@eng2.sequent.com> 
  20655. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20656. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  20657. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  20658. Date: Thu, 09 Jul 1992 10:06:14 +0200
  20659. Sender: Guido.van.Rossum@cwi.nl
  20660.  
  20661. Jaap Vermeulen <jaap@sequent.com> writes:
  20662.  
  20663. >I was looking around for a listbox inside a window.  I can't find any
  20664. >example such a thing (maybe I'm just overlooking it?).  Mainly I want
  20665. >to position two listboxes (or scrollboxes or whatever you wanna call
  20666. >'m) within a single window.
  20667.  
  20668. There isn't any, but DirList is indeed a good example for how to build
  20669. one out of buttons and splits.  Note, however, that all these
  20670. widget-type things were part of a project that I never finished (hence
  20671. the abhorring lack of documentation :-).  One of the reasons was
  20672. indeed that it felt too slow to be useful in practice.  Another reason
  20673. was that I never could invent a really simple programmer interface,
  20674. either with or without multiple inheritance.  Maybe this widget stuff
  20675. isn't easy as I thought?  The serious window systems [Andrew, X11,
  20676. Windows3.1] are all too big, and the small ones [e.g. Rob Pike's one
  20677. for Plan 9] suffer from lack of flexibility...
  20678.  
  20679. >% time Python TestDirList.py
  20680. >
  20681. >Time: 2:28.41 total  137.090 user  4.080 system  95.1% CPU
  20682. >IO:   59+1 io  391+3 r/w syscalls  0+0 raw r/w
  20683. >Mem:  0+14058 maj/min pf  137+344 incr/decr pff  0 max 187 zero 0 swaps
  20684. >Sys:  147+1 vol/invol ctxsw  2306 system calls  0 signals
  20685.  
  20686. I also played around with it a little and found this, opening 7 or 8
  20687. directories in a minute:
  20688.  
  20689. |& time python TestDirList.py
  20690. |
  20691. |real     1:10.1
  20692. |user        3.6
  20693. |sys         0.8
  20694. |&
  20695.  
  20696. Now this is on a 33 MHz SGI Indigo, a real racing car.  Is your
  20697. machine slow or memory-starved?  (14058 minor page faults looks like a
  20698. lot to me).
  20699.  
  20700. On the other hand it could be that your realloc() implementation is
  20701. slow.  If this is the case, building lists by appending elements to
  20702. the end one by one (as is done by os.listdir(), for example) may
  20703. become an O(n**2) matter since realloc() is copying the entire array
  20704. on each expansion.  Most current malloc packages are cleverer than
  20705. that, so I didn't bother to try and optimize this in the Python code,
  20706. but if you're adventurous you could dive into the "listobject" source
  20707. code and somehow extend the array used to hold the list in larger
  20708. chunks.
  20709.  
  20710. Of course, I'd try to profile python first, just to get more insight
  20711. (and yes, even on my system malloc is on top of the list).
  20712.  
  20713. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  20714. "Anyone capable to get themselves elected president should on no
  20715. account be allowed to do the job."
  20716. 
  20717. 
  20718. Replied: Thu, 09 Jul 1992 23:53:52 +0200
  20719. Replied: Python Mailing List <python-list@cwi.nl>
  20720. Received: from [138.95.19.12] by charon.cwi.nl with SMTP
  20721.     id AA16482 (5.65b/2.10/CWI-Amsterdam); Thu, 9 Jul 1992 19:50:45 +0200
  20722. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  20723.     id AA04224; Thu, 9 Jul 92 10:48:34 -0700
  20724. Received: by eng2.sequent.com (5.65/1.34)
  20725.     id AA09779; Thu, 9 Jul 92 10:50:37 -0700
  20726. Message-Id: <9207091750.AA09779@eng2.sequent.com>
  20727. To: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20728. Cc: Python Mailing List <python-list@cwi.nl>
  20729. Subject: Re: python and stdwin: question and observation 
  20730. Priority: Normal
  20731. Precedence: first-class
  20732. Organization: Sequent Computer Systems, Inc.
  20733.               Decision Support Group - MailStop: RHE2-358
  20734.           15450 S.W. Koll Parkway
  20735.               Beaverton, OR  97006
  20736. X-Phone: (503) 578-4404
  20737. X-Fax: (503) 578-7569
  20738. X-Uucp: ...!uunet!sequent!jaap
  20739. X-Internet: jaap@sequent.com
  20740. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  20741.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  20742.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  20743. In-Reply-To: Guido van Rossum's message of Thu, 09 Jul 92 10:06:14 +0200.
  20744.              <9207090806.AA21387@voorn.cwi.nl> 
  20745. Date: Thu, 09 Jul 92 10:50:36 PDT
  20746. From: Jaap Vermeulen <jaap@sequent.com>
  20747.  
  20748.  
  20749. | There isn't any, but DirList is indeed a good example for how to build
  20750. | one out of buttons and splits.  Note, however, that all these
  20751.  
  20752. The problem, though, is that I don't know how to put multiple DirLists
  20753. in a single window. Or is it plain impossible? (In that case: bye,
  20754. bye, stdwin. :-)
  20755.  
  20756. | either with or without multiple inheritance.  Maybe this widget stuff
  20757. | isn't easy as I thought?  The serious window systems [Andrew, X11,
  20758. | Windows3.1] are all too big, and the small ones [e.g. Rob Pike's one
  20759. | for Plan 9] suffer from lack of flexibility...
  20760.  
  20761. What I'm missing is a high level abstraction model for "accepted" user
  20762. interface objects, such as dialog boxes with elements such as a listbox.
  20763.  
  20764. | I also played around with it a little and found this, opening 7 or 8
  20765. | directories in a minute:
  20766. | |& time python TestDirList.py
  20767. | |
  20768. | |real     1:10.1
  20769. | |user        3.6
  20770. | |sys         0.8
  20771. | |&
  20772. | Now this is on a 33 MHz SGI Indigo, a real racing car.  Is your
  20773. | machine slow or memory-starved?  (14058 minor page faults looks like a
  20774. | lot to me).
  20775.  
  20776. I'm running on a 12 processor, 300Mb symmetry, and the load average is
  20777. below .5 with ~60 people. It's not the fastest machine, but certainly
  20778. nearly 100% processor utilization time (which means I used one of the
  20779. twelve processors almost exclusively; and note: the utilization time
  20780. can go as high as 1200%) is very suspicious...
  20781.  
  20782. | On the other hand it could be that your realloc() implementation is
  20783. | slow.  If this is the case, building lists by appending elements to
  20784.  
  20785. Now *that* is an interesting assumption. I compiled in the optional
  20786. malloc library which implements the bucket algorithm. And lo and behold,
  20787. it is much better, but not close to what you show. It lookes like the
  20788. size of the directory is also pretty important... Maybe there are malloc
  20789. packages that are *really* smart? Here are some memory statistics:
  20790.  
  20791. Process: python  pid: 8853  text_handle: 0x01b8f364  KB pte: 12
  20792.                       Values in K Bytes
  20793. Segment       Virtual  Resident  Weighted RSS <- this is the only process
  20794. text            408        208      208.00     running, so there is nothing
  20795. data+bss       1236        880      880.00     to weigh (otherwise text
  20796. stack            12         12       12.00     would be shared)
  20797. shared            0          0        0.00
  20798.  
  20799. Total          1656       1100     1100.00
  20800.  
  20801. FYI: the paging algorithms on this system are set up to let processes
  20802.      go up to 3Mb before *really* pruning them.
  20803.  
  20804. | the end one by one (as is done by os.listdir(), for example) may
  20805. | become an O(n**2) matter since realloc() is copying the entire array
  20806. | on each expansion.  Most current malloc packages are cleverer than
  20807. | that, so I didn't bother to try and optimize this in the Python code,
  20808. | but if you're adventurous you could dive into the "listobject" source
  20809. | code and somehow extend the array used to hold the list in larger
  20810. | chunks.
  20811.  
  20812. No time for adventures right now. :-)
  20813.  
  20814. | Of course, I'd try to profile python first, just to get more insight
  20815. | (and yes, even on my system malloc is on top of the list).
  20816.  
  20817. OK, here is what I got (only the highest entries on the list):
  20818.  
  20819.  %Time Seconds Cumsecs  #Calls   msec/call  Name
  20820.   21.6   16.58   16.58   23698      0.6997  newdictobject
  20821.   15.6   11.97   28.56   23182      0.5165  eval_code
  20822.   10.5    8.03   36.58  262848      0.0305  malloc
  20823.   10.4    7.97   44.55                      _mcount
  20824.    4.8    3.70   48.25  262277      0.0141  free
  20825.    3.4    2.58   50.83  582599      0.0044  strcmp
  20826.    3.3    2.52   53.35   34252      0.0736  call_object
  20827.    2.4    1.86   55.22     503      3.71    stat
  20828.    1.8    1.36   56.58   94046      0.0145  settupleitem
  20829.    1.8    1.35   57.93  183854      0.0074  dict2lookup
  20830.    1.7    1.34   59.27  107153      0.0125  dict2insert
  20831.    1.6    1.21   60.48      30     40.3     newclassobject
  20832.    1.2    0.93   61.41  181919      0.0051  gettupleitem
  20833.    1.1    0.84   62.25     877      0.96    getdictkey
  20834.    1.0    0.75   63.00  158436      0.0047  gettuplesize
  20835.    1.0    0.75   63.75   98732      0.0076  cmpobject
  20836.    1.0    0.73   64.48    1182      0.618   putmsg
  20837.  
  20838. malloc is *not* on the top of the list. :-) Is this normal?  Any other
  20839. input as to why it may be so cpu intensive?
  20840.  
  20841. Thanks,
  20842.  
  20843.     -Jaap-
  20844. --
  20845. Jaap Vermeulen                    +--------------------------+
  20846.                         | Sequent Computer Systems |
  20847.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  20848.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  20849. 
  20850. 
  20851. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  20852.     id AA21647 (5.65b/2.10/CWI-Amsterdam); Thu, 9 Jul 1992 23:53:46 +0200
  20853. Received: by voorn.cwi.nl with SMTP; Thu, 9 Jul 1992 21:53:51 GMT
  20854. Message-Id: <9207092153.AA24876@voorn.cwi.nl>
  20855. To: Python Mailing List <python-list@cwi.nl>
  20856. Subject: Re: python and stdwin: question and observation 
  20857. In-Reply-To: Your message of "Thu, 09 Jul 1992 10:50:36 MDT."
  20858.              <9207091750.AA09779@eng2.sequent.com> 
  20859. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20860. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  20861. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  20862. Date: Thu, 09 Jul 1992 23:53:50 +0200
  20863. Sender: Guido.van.Rossum@cwi.nl
  20864.  
  20865. Jaap Vermeulen <jaap@sequent.com> writes:
  20866.  
  20867. >The problem, though, is that I don't know how to put multiple DirLists
  20868. >in a single window. Or is it plain impossible? (In that case: bye,
  20869. >bye, stdwin. :-)
  20870.  
  20871. Check out the HVSplit module, this lets you build an arbitrary widget
  20872. tree.
  20873.  
  20874. >What I'm missing is a high level abstraction model for "accepted" user
  20875. >interface objects, such as dialog boxes with elements such as a listbox.
  20876.  
  20877. There are remarkably many of these and it is remarkably hard to make
  20878. them general enough -- again, see the size of Motif and Windows.
  20879.  
  20880. >Now *that* is an interesting assumption. I compiled in the optional
  20881. >malloc library which implements the bucket algorithm. And lo and behold,
  20882. >it is much better, but not close to what you show. It lookes like the
  20883. >size of the directory is also pretty important...
  20884.  
  20885. Of course it is, if the algorithm is O(n**2)...  Unfirtunately I have
  20886. little idea of what to do about this particular problem.  Anybody
  20887. know about a really fast malloc library?  I seem to remember that the
  20888. best ones keep separate free lists per size, rounded to powers of two.
  20889.  
  20890. > %Time Seconds Cumsecs  #Calls   msec/call  Name
  20891. >  21.6   16.58   16.58   23698      0.6997  newdictobject
  20892. >  15.6   11.97   28.56   23182      0.5165  eval_code
  20893. >  10.5    8.03   36.58  262848      0.0305  malloc
  20894. >  10.4    7.97   44.55                      _mcount
  20895. >   4.8    3.70   48.25  262277      0.0141  free
  20896. >   3.4    2.58   50.83  582599      0.0044  strcmp
  20897. >   3.3    2.52   53.35   34252      0.0736  call_object
  20898.  
  20899. Hmm, the newdictobject on top is suspicious, it doesn't do anything
  20900. that could take 0.7 msec.  I suspect its time may include the time
  20901. taken by lookdict(), the next function in the file which happens to be
  20902. static and is used for every local variable reference (most calls to
  20903. strcmp() come from there I would guess).  Don't you have a more modern
  20904. profiler, e.g. gprof?  The number of eval_code calls (the main
  20905. interpreter loop) reflects mostly the number of function calls, and
  20906. most newdictobject() calls are for the dictionaries of local variables.
  20907. (I suppose there's an opportunity for a code optimizer here. :-)
  20908.  
  20909. That's my two cents worth for now...
  20910.  
  20911. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  20912. "Did you say knives?"
  20913. "Rotating knives, yes"
  20914. 
  20915. 
  20916. Replied: Sun, 12 Jul 1992 12:53:56 +0200
  20917. Replied: python-list@cwi.nl
  20918. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  20919.     id AA22292 (5.65b/2.10/CWI-Amsterdam); Sun, 12 Jul 1992 01:58:02 +0200
  20920. Received: from stun4q.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  20921.     id AA26328; Sat, 11 Jul 92 16:56:50 PDT
  20922. Date: Sat, 11 Jul 92 16:56:50 PDT
  20923. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  20924. Message-Id: <9207112356.AA26328@jove.cs.caltech.edu>
  20925. Received: by stun4q.cs.caltech.edu (4.1/SMI-4.1)
  20926.     id AA14315; Sat, 11 Jul 92 16:58:03 PDT
  20927. To: python-list@cwi.nl
  20928. Subject: Is delayed evaluation possible ?
  20929. Comments: Hyperbole mail buttons accepted, v3.02P.
  20930.  
  20931. I am doing a large amount of substitution of variables into LONG strings in
  20932. python. The length of these strings is making my code unreadable.
  20933.  
  20934. 2 questions:
  20935. Is it possible to alias a string that will have variables substituted
  20936. into it if the variables are not already defined such as:
  20937.  
  20938. xdr_stream.append('xdr_' dataType + \
  20939.     '(&(M_RemoteProcessPointer->xdr_write), \
  20940.     + rpc_arg + '); \n)
  20941.  
  20942. can i do substr = ('xdr_' dataType + \
  20943.     '(&(M_RemoteProcessPointer->xdr_write), \
  20944.     + rpc_arg + '); \n)
  20945.  
  20946. xdr.stream.append(substr) so that I dont have to ugly up my code ?
  20947.  
  20948. This also has another question built into it as to whether or not
  20949. unbound variables in strings are possible through the use of delayed
  20950. evaluation. This is a nice feature that Tcl has.
  20951.  
  20952.  
  20953.  
  20954. --
  20955. terrence brannon
  20956. brannon@jove.cs.caltech.edu 
  20957.  
  20958. 
  20959. 
  20960. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  20961.     id AA27355 (5.65b/2.10/CWI-Amsterdam); Sun, 12 Jul 1992 12:53:52 +0200
  20962. Received: by voorn.cwi.nl with SMTP; Sun, 12 Jul 1992 10:53:55 GMT
  20963. Message-Id: <9207121053.AA29602@voorn.cwi.nl>
  20964. To: python-list@cwi.nl
  20965. Subject: Re: Is delayed evaluation possible ? 
  20966. In-Reply-To: Your message of "Sat, 11 Jul 1992 16:56:50 MDT."
  20967.              <9207112356.AA26328@jove.cs.caltech.edu> 
  20968. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  20969. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  20970. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  20971. Date: Sun, 12 Jul 1992 12:53:54 +0200
  20972. Sender: Guido.van.Rossum@cwi.nl
  20973.  
  20974. brannon@jove.cs.caltech.edu (Terrence M. Brannon) writes:
  20975.  
  20976. >Is it possible to alias a string that will have variables substituted
  20977. >into it if the variables are not already defined such as:
  20978. >
  20979. >xdr_stream.append('xdr_' dataType + \
  20980. >    '(&(M_RemoteProcessPointer->xdr_write), \
  20981. >    + rpc_arg + '); \n)
  20982. >
  20983. >can i do substr = ('xdr_' dataType + \
  20984. >    '(&(M_RemoteProcessPointer->xdr_write), \
  20985. >    + rpc_arg + '); \n)
  20986. >
  20987. >xdr.stream.append(substr) so that I dont have to ugly up my code ?
  20988. >
  20989. >This also has another question built into it as to whether or not
  20990. >unbound variables in strings are possible through the use of delayed
  20991. >evaluation. This is a nice feature that Tcl has.
  20992.  
  20993. No, there is no delayed evaluation of strings in Python, but you can
  20994. do what you want using a function (which is of course the more general
  20995. way of expressing delayed evaluation of anything):
  20996.  
  20997. def substr(dataType, rpc_arg):
  20998.     return 'xdr_' + dataType + \
  20999.        '(&(M_RemoteProcessPointer->xdr_write), ' \
  21000.        + rpc_arg + '); \n'
  21001.  
  21002. (note that I have supplied some missing quotes and operators in your
  21003. example :-) and then write
  21004.  
  21005. xdr.stream.append(substr(dataType, rpc_arg)).
  21006.  
  21007. You may be able to shorten your code even more if you change the
  21008. function arguments into global variables -- personally I don't like
  21009. the use of globals when arguments could be used, and Python reflects
  21010. this view by forcing you to declare your globals as such in every
  21011. function that assigns to them.
  21012.  
  21013. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  21014. "Exploding is a perfectly normal medical phenomenon."
  21015.  
  21016. 
  21017. 
  21018. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  21019.     id AA28738 (5.65b/2.10/CWI-Amsterdam); Sun, 12 Jul 1992 18:08:20 +0200
  21020. Received: from stun4q.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  21021.     id AA26704; Sun, 12 Jul 92 09:07:04 PDT
  21022. Date: Sun, 12 Jul 92 09:07:03 PDT
  21023. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  21024. Message-Id: <9207121607.AA26704@jove.cs.caltech.edu>
  21025. Received: by stun4q.cs.caltech.edu (4.1/SMI-4.1)
  21026.     id AA14370; Sun, 12 Jul 92 09:08:21 PDT
  21027. To: python-list@cwi.nl
  21028. Subject: A question on the direction of Python
  21029. Comments: Hyperbole mail buttons accepted, v3.02P.
  21030.  
  21031. I notice I have to do certain things by myself in Python that it would
  21032. be nice if there were registers for. The one example I keep running
  21033. into right now is if I have a loop like this:
  21034.  
  21035. tmp = open('file','r').readlines()
  21036. iterationCount=0
  21037. for line in tmp:
  21038.     iterationCount = iterationCount + 1
  21039.     if regex.match('adfasdf',line) > 5
  21040.         break
  21041.     else:
  21042.         serverloop_code.insert(iterationCount,line)  # line X
  21043.  
  21044. It would be nice if iterationCount were a pre-defined register like @i
  21045. or something so the code could be more compact . However, it would be
  21046. more cryptic to someone just getting into the language. Registers were
  21047. the main thing that made me quit learning Perl quickly.
  21048.  
  21049. Another way to look at this is that since Python is an object-oriented
  21050. language the for statement above is sending a message to `for' with 2
  21051. arguments. Since objects have their own private data, it would seem
  21052. that we could type serverloop_code.insert(for.current_pos(), line) at
  21053. line X to access some data in the `for' object.
  21054.  
  21055. What do you all think ?
  21056.  
  21057. --
  21058. terrence brannon
  21059. brannon@jove.cs.caltech.edu 
  21060.  
  21061. 
  21062. 
  21063. Received: from uunet.ca by charon.cwi.nl with SMTP
  21064.     id AA29511 (5.65b/2.10/CWI-Amsterdam); Sun, 12 Jul 1992 20:07:34 +0200
  21065. Received: from tslwat by mail.uunet.ca with UUCP id <9677>; Sun, 12 Jul 1992 14:07:32 -0400
  21066. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  21067.     id <m0m78Nd-0001PiC@research.teleride.on.ca>; Sun, 12 Jul 92 14:10 EDT
  21068. Message-Id: <m0m78Nd-0001PiC@research.teleride.on.ca>
  21069. From: louk@research.teleride.on.ca (Lou Kates)
  21070. Subject: Re: iteration count register
  21071. To: python-list@cwi.nl
  21072. Date:     Sun, 12 Jul 1992 14:10:41 -0400
  21073. X-Mailer: ELM [version 2.3 PL11]
  21074.  
  21075. Forwarded message:
  21076. > From:    brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  21077. > I notice I have to do certain things by myself in Python that it would
  21078. > be nice if there were registers for. The one example I keep running
  21079. > into right now is if I have a loop like this:
  21080. > tmp = open('file','r').readlines()
  21081. > iterationCount=0
  21082. > for line in tmp:
  21083. >     iterationCount = iterationCount + 1
  21084. >     if regex.match('adfasdf',line) > 5
  21085. >         break
  21086. >     else:
  21087. >         serverloop_code.insert(iterationCount,line)  # line X
  21088. > It would be nice if iterationCount were a pre-defined register like @i
  21089. > or something so the code could be more compact . However, it would be
  21090. > more cryptic to someone just getting into the language. Registers were
  21091. > the main thing that made me quit learning Perl quickly.
  21092. > Another way to look at this is that since Python is an object-oriented
  21093. > language the for statement above is sending a message to `for' with 2
  21094. > arguments. Since objects have their own private data, it would seem
  21095. > that we could type serverloop_code.insert(for.current_pos(), line) at
  21096. > line X to access some data in the `for' object.
  21097. > What do you all think ?
  21098. > --
  21099. > terrence brannon
  21100. > brannon@jove.cs.caltech.edu 
  21101.  
  21102. > tmp = open('file','r').readlines()
  21103. > iterationCount=0
  21104. > for line in tmp:
  21105. >     iterationCount = iterationCount + 1
  21106. >     if regex.match('adfasdf',line) > 5
  21107. >         break
  21108. >     else:
  21109. >         serverloop_code.insert(iterationCount,line)  # line X
  21110.  
  21111. Your OO solution seems neat.
  21112.  
  21113. Note that  if your only objective  is reduction in code size then
  21114. the above  example  could  be reduced   to the  same size as  the
  21115. perlized  python  solution by  iterating over an index instead of
  21116. over the lines themselves:
  21117.  
  21118.     lines = open('file','r').readlines()
  21119.     for i in range(len(lines)):
  21120.         if regex.match('adfasdf',lines[i]) > 5:
  21121.             break
  21122.         else:
  21123.             serverloop_code.insert(i,lines[i])
  21124.  
  21125. My example still has a few problems:
  21126.  
  21127. -    you have  to  generate the   range(len(lines)) list. If  the
  21128.      number of lines is large this seems rather wasteful.
  21129.  
  21130. -    if the  number  of lines   were unbounded then the  previous
  21131.      point is  even worse and you have to introduce a number HUGE
  21132.      which is unpleasant:
  21133.  
  21134.     for i in range(HUGE):
  21135.  
  21136. -    there is  an extra array  reference (lines[i]) which adds to
  21137.      complexity
  21138.  
  21139. On the  other hand  this solution  has one less variable than the
  21140. perlized python  solution and the  variable it eliminates is  the
  21141. builtin register variable which is the one most onerous.
  21142.     
  21143.  
  21144. -- 
  21145. Lou Kates, louk@research.teleride.on.ca
  21146. 
  21147. 
  21148. Replied: Tue, 14 Jul 1992 10:25:26 +0200
  21149. Replied: "python-list@cwi.nl "
  21150. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  21151.     id AA22963 (5.65b/2.10/CWI-Amsterdam); Tue, 14 Jul 1992 03:08:58 +0200
  21152. Received: from stun4b.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  21153.     id AA28703; Mon, 13 Jul 92 18:07:31 PDT
  21154. Date: Mon, 13 Jul 92 18:07:31 PDT
  21155. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  21156. Message-Id: <9207140107.AA28703@jove.cs.caltech.edu>
  21157. Received: by stun4b.cs.caltech.edu (4.1/SMI-4.1)
  21158.     id AA04784; Mon, 13 Jul 92 18:08:58 PDT
  21159. To: python-list@cwi.nl
  21160. Subject: Inital attempt to make emacs an object
  21161. Comments: Hyperbole mail buttons accepted, v3.02P.
  21162.  
  21163. The next two posts are my first attempt to create communication
  21164. between emacs and python.
  21165.  
  21166. Currently a python script which imports the emacs.py module will be
  21167. able to issue these three commands:
  21168.  
  21169. emacs.dired        ex: emacs.dired('.')  emacs.dired('~')
  21170. emacs.buffer_menu   ex: emacs.buffer_menu()
  21171. emacs.eval          use any valid elisp form. outermost parenths optional
  21172.             ex: emacs.eval('copy-file "/anonymous@cwi.nl:x" "y" t)
  21173.                 : emacs.eval('message "hi there"')
  21174.                 : emacs.eval('(progn (find-file "source.el") (eval-current-buffer))')
  21175.  
  21176.  
  21177. The next file is the python script.
  21178. The file after that is the emacs lisp.
  21179.  
  21180. Follow the directions in the emacs lisp source.
  21181.  
  21182. --
  21183. terrence brannon
  21184. brannon@jove.cs.caltech.edu 
  21185.  
  21186. 
  21187. 
  21188. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  21189.     id AA23155 (5.65b/2.10/CWI-Amsterdam); Tue, 14 Jul 1992 03:23:27 +0200
  21190. Received: from stun4b.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  21191.     id AA28839; Mon, 13 Jul 92 18:21:44 PDT
  21192. Date: Mon, 13 Jul 92 18:21:43 PDT
  21193. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  21194. Message-Id: <9207140121.AA28839@jove.cs.caltech.edu>
  21195. Received: by stun4b.cs.caltech.edu (4.1/SMI-4.1)
  21196.     id AA04786; Mon, 13 Jul 92 18:23:10 PDT
  21197. To: python-list@cwi.nl
  21198. Subject: emacs.py
  21199. Comments: Hyperbole mail buttons accepted, v3.02P.
  21200.  
  21201. # Initial attempt to make emacs a useable object of python
  21202. # This code will be imported from a python interpreter that is running
  21203. # as an inferior process of emacs
  21204.  
  21205. start_marker = '+'
  21206. end_marker   = '~'
  21207.  
  21208. def eval (string):
  21209.     tmpstr = start_marker + '(' + string + ')' + end_marker
  21210.     print tmpstr
  21211. print 'method eval(string):'
  21212.  
  21213. def dired (directory):
  21214.     eval( 'dired ' + '"' + directory + '"' )
  21215. print 'method dired(directory):'
  21216.  
  21217. def buffer_menu ():
  21218.     eval( 'buffer-menu(buffer-list)' )
  21219. print 'method buffer_menu():'
  21220.  
  21221.  
  21222. print 'module py_emacs loaded.'
  21223. 
  21224. 
  21225. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  21226.     id AA23169 (5.65b/2.10/CWI-Amsterdam); Tue, 14 Jul 1992 03:24:23 +0200
  21227. Received: from stun4b.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  21228.     id AA28842; Mon, 13 Jul 92 18:22:57 PDT
  21229. Date: Mon, 13 Jul 92 18:22:57 PDT
  21230. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  21231. Message-Id: <9207140122.AA28842@jove.cs.caltech.edu>
  21232. Received: by stun4b.cs.caltech.edu (4.1/SMI-4.1)
  21233.     id AA04787; Mon, 13 Jul 92 18:24:24 PDT
  21234. To: python-list@cwi.nl
  21235. Subject: py-connect.el
  21236. Comments: Hyperbole mail buttons accepted, v3.02P.
  21237.  
  21238. (require 'comint)
  21239. ;;; python-connect.el
  21240. ;;; Project
  21241. ;;; Allow the creation of multiple emacs objects which can be queried
  21242. ;;; and commanded. Primary reason is to use ange-ftp from python since
  21243. ;;; it is a big headache to re-code all of ange-ftp in python - plus
  21244. ;;; why re-invent the wheel.
  21245. ;;; 
  21246. ;;; Technique
  21247. ;;; Basically we spawn a python interpreter as an inferior process of
  21248. ;;; emacs and then filter its output . If we get a string delimited
  21249. ;;; by the start and end markers then we evaluate it.
  21250. ;;;
  21251. ;;; Requirements
  21252. ;;; comint.el and cmushell.el both available from:
  21253. ;;; "/anonymous@archive.cis.ohio-state.edu:pub/gnu/emacs/elisp-archive/packages"
  21254. ;;; python, available from:
  21255. ;;; "/anonymous@ftp.cwi.nl:pub"
  21256. ;;;
  21257. ;;; Usage
  21258. ;;; C-x C-f python.connect.el (or whatever you name this)
  21259. ;;; M-x eval-current-buffer
  21260. ;;; M-x py-connect
  21261. ;;; At the python prompt (>>>) type import emacs (if you named the
  21262. ;;; python script emacs.py -- be sure it is on your load path)
  21263. ;;; Now at the python prompt you may call any of the defined modules.
  21264. ;;; ex: emacs.dired('.')
  21265. ;;; You may also type elisp strings delimited by + and ~ and they will
  21266. ;;; be evaluated in emacs after you hit return.
  21267. ;;; ex: +(copy-file "/anonymous@princeton.edu:yoga" "my-yoga" t)~
  21268. ;;;
  21269. ;;; SCOPES FOR IMPROVEMENT
  21270. ;;; an extremely robust interface would allow one to do completion by
  21271. ;;; sending an obarray/assoc list and complete-file-name command to
  21272. ;;; emacs . then get back the results
  21273. ;;;
  21274. ;;; Another project
  21275. ;;; Another possibility is to make it so you can run python without it being
  21276. ;;; on your local machine - just telnet to the Python interpreter port and
  21277. ;;; tell it whether you are using x-windows or whatnot.
  21278. ;;; - games
  21279. ;;; - demos . see where they are calling from . ange-ftp a file to their
  21280. ;;;   home directory
  21281. ;;; - portmapper
  21282. ;;;
  21283. ;;; Another project
  21284. ;;; NOTE: remote emacs-code becomes executable if it shows up in a buffer
  21285. ;;; because emacs has a built in interpreter . if we have
  21286. ;;; self-intelligent interpreters, they can show up on your remote
  21287. ;;; machine and do their work, remember the results, and leave. just
  21288. ;;; tell the objects what to do and they head over to the correct
  21289. ;;; machine and do it
  21290.  
  21291. (setq start-mkr ?+)
  21292. (setq start-mkr-string (char-to-string ?+))
  21293. (setq end-mkr-string (char-to-string ?~))
  21294.  
  21295. (setq accum-string "")
  21296.  
  21297. (defun py-connect ()
  21298.   (interactive)
  21299.   (cond ((not (comint-check-proc "*py-connect*"))
  21300.      (let* ((prog "python")
  21301.         (name (file-name-nondirectory prog)))
  21302.        (set-buffer (apply 'make-comint "py-connect" (list prog)))
  21303.        (comint-mode))))
  21304.   (switch-to-buffer "*py-connect*")
  21305.   (setq comint-prompt-regexp ">>>")
  21306.   (setq P (get-buffer-process (current-buffer)))
  21307.   (set-process-filter P 'elisp-filter))
  21308.  
  21309. (defun elisp-filter (process string)
  21310.   "Make sure that the window continues to show the most recently output
  21311. text."
  21312.  
  21313.   (let ((old-buffer (current-buffer)))
  21314.     (unwind-protect
  21315.     (let (moving)
  21316.       (set-buffer (process-buffer process))
  21317.       (setq moving (= (point) (process-mark process)))
  21318.       (save-excursion
  21319.         (goto-char (process-mark process))
  21320.         (insert string)
  21321.         (set-marker (process-mark process) (point)))
  21322.       (if moving (goto-char (process-mark process))))
  21323.       (set-buffer old-buffer)))
  21324.  
  21325.   (while (not (string-equal string ""))
  21326.     (let ((s (string-match start-mkr-string string))
  21327.       (e (string-match end-mkr-string string)))
  21328.       (if (string-equal accum-string "")
  21329.       (progn
  21330.         (setq expecting 'start-marker)
  21331.         (if s
  21332.         (progn
  21333.           (setq expecting 'end-marker)
  21334.           (if e 
  21335.               (progn
  21336.             (interp-elisp (substring string (1+ s) e))
  21337.             (setq string (substring string (1+ e))))
  21338.             (progn
  21339.               (setq accum-string (substring string (1+ s)))
  21340.               (setq string ""))))
  21341.           (setq string "")))
  21342.     (if e
  21343.         (progn
  21344.           (interp-elisp (concat accum-string (substring string 0 (1- e))))
  21345.           (setq string (substring string (1+ e))))
  21346.       (setq accum-string (concat accum-string string))
  21347.       (setq string ""))))))
  21348.     
  21349.   
  21350. (defun interp-elisp (string)
  21351.   (eval (read string)))
  21352.           
  21353. ;;; hacking from here on out -- ignore (or enjoy)
  21354.  
  21355.  
  21356. (defun filter-accum-command (string)
  21357.   "This function is only called to reset the systems idea of the
  21358. currently accumulated output from the process if output is already in
  21359. accum-string and possibly evaluate this output if end-mkr is seen.
  21360. What it does is check for the first start-mkr and end-mkr in the
  21361. string. If neither exists, <return> the concatenation of string onto
  21362. accum-string and <exit>. If start-mkr is first, then the accumulated
  21363. string is reset to STRING from start-mkr+1 onward and we recursively
  21364. call this function. If end-mkr is first, then we evaluate (concat
  21365. accum-string STRING up-to-end-mkr), search for the next start-mkr. If
  21366. no start-mkr, <return> empty quotes and <exit>. If a start-mkr,
  21367. <return> from start-mkr to end of STRING and <exit>"
  21368.   (debug)
  21369.   (let* ((s (string-match start-mkr-string string))
  21370.      (e (string-match   end-mkr-string string)))
  21371.     (cond 
  21372.      ((and (not s) (not e)) 
  21373.       (concat accum-string string))
  21374.      ((or (< s e) (and s (not e)))
  21375.       (filter-accum-command (substring (1+ s) string)))
  21376.      ((or (< e s) (and e (not s)))
  21377.       (progn
  21378.     (interpret-elisp (concat accum-string (substring 0 (1- e)
  21379.                              string)))
  21380.     (setq string (substring (1+ e) string))
  21381.     (let ((st (string-match start-mkr-string string)))
  21382.       (if st
  21383.           (substring string (1+ st))
  21384.         ""))
  21385.     )
  21386.       )
  21387.      )
  21388.     )
  21389.   )
  21390.  
  21391. (defun interpret-elisp (str)
  21392.   (message str)
  21393.   (eval (car (read-from-string str))))
  21394.  
  21395.  
  21396.  
  21397. (defun old-elisp-filter (proc string)
  21398.   (interactive)
  21399.   ;;; Cases -
  21400.   ;;;
  21401.   ;;; (0) if accum-string == "", (0.5) expect a start marker. 
  21402.   ;;; (1.0) accum-string will equal start-marker to finish-marker or
  21403.   ;;; start-marker to end of string. if accum-string went to end of
  21404.   ;;; string, exit. (1.5) else eval the region, chop the region from the
  21405.   ;;; string, set  
  21406.   ;;; accum-string to "" and retry (0)
  21407.   ;;; (2) if accum-string != "", expect a finish marker. if we get a
  21408.   ;;; finish marker, (2.5) concat accum string and string up to finish
  21409.   ;;; marker, evaluate then pass the remainder of the stream to (0).
  21410.   ;;; (3.0) if we get a start-marker, set accum-string to "" , prepend
  21411.   ;;; start-marker to string and goto (0). (3.5) if we get neither, append
  21412.   ;;; all of string to accum-string, set accum-string to "", then exit
  21413.  
  21414.   (if (string-equal accum-string "")
  21415.       (case-0 string)
  21416.     (case-2 string)))
  21417.  
  21418. (defun case-0 (string)
  21419.   (let ((count 0)
  21420.     (lth (length string)))
  21421.     (while (not (equal (elt string count) start-mkr))
  21422.         (setq count (1+ count)))
  21423.     (if (equal count lth)
  21424.     ()
  21425.       (progn
  21426.     (setq accum-string (do-1-0))
  21427.     (if eos
  21428.         ()
  21429.       (progn
  21430.         (do-1-5)
  21431.         (setq accum-string "")
  21432.         (case-0 string)))))))
  21433.  
  21434. (defun case-2 (string)
  21435.   (let ((count 0)
  21436.     (lth (length string)))
  21437.     (while (not (or (equal (elt string count) start-mkr)
  21438.             (equal (elt string count) end-mkr)))
  21439.       (setq count (1+ count)))
  21440.     (if (equal count lth)
  21441.     (do-3-5))
  21442.     (if (equal (elt string count) end-mkr)
  21443.     (progn
  21444.       (setq string (do-2-5 string))
  21445.       (setq accum-string "")
  21446.       (case-0 string))
  21447.       (progn
  21448.     (setq accum-string "")
  21449.     (setq newstr (concat start-mkr string))
  21450.     (case-0 newstr)))))
  21451.   
  21452.  
  21453. ;(comint-proc-query P "\na = [2 , 3, 4] \n")
  21454.  
  21455. (defun t ()
  21456.   (interactive)
  21457.   (pop-to-buffer "*py-connect*")
  21458.   (setq comint-eol-on-send 't)
  21459.   (visi-input "a = [ 4 ,5 ,6 ] ")
  21460.   (visi-input "a"))
  21461.  
  21462. (defun visi-input (string)
  21463.   (message string)
  21464.   (sit-for 1)
  21465.   (goto-char (point-max))
  21466.   (let* ((end (point))
  21467.      (start (progn
  21468.           (beginning-of-line 1)
  21469.           (point)))
  21470.      (line (buffer-substring start end)))
  21471.     (while (not (string-match "[ ]*" line))
  21472.  
  21473.       (setq end (point))
  21474.       (setq start (progn
  21475.             (beginning-of-line 1)
  21476.             (point)))
  21477.       (setq line (buffer-substring start end)))
  21478.     (goto-char (point-max))
  21479.     (insert string)
  21480.     (comint-send-input)
  21481.     (goto-char (point-max))))
  21482.     
  21483.  
  21484.  
  21485.  
  21486.  
  21487. 
  21488. 
  21489. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  21490.     id AA24844 (5.65b/2.10/CWI-Amsterdam); Tue, 14 Jul 1992 04:55:46 +0200
  21491. Received: from stun4b.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  21492.     id AA29160; Mon, 13 Jul 92 19:54:20 PDT
  21493. Date: Mon, 13 Jul 92 19:54:20 PDT
  21494. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  21495. Message-Id: <9207140254.AA29160@jove.cs.caltech.edu>
  21496. Received: by stun4b.cs.caltech.edu (4.1/SMI-4.1)
  21497.     id AA04803; Mon, 13 Jul 92 19:55:48 PDT
  21498. To: python-list@cwi.nl
  21499. Subject: Re:  A question on the direction of Python
  21500. Comments: Hyperbole mail buttons accepted, v3.02P.
  21501.  
  21502. ------- Start of forwarded message -------
  21503. Re: Iteration counts.
  21504.  
  21505. If you need the index, then use an indexed for using range() and len() as 
  21506. generators:
  21507.  
  21508. >>> for i in range( 0, len( tmp )): 
  21509. ...        { do something to tmp[i] } 
  21510.  
  21511.  
  21512. > It would be nice if iterationCount were a pre-defined register like @i
  21513. > or something so the code could be more compact . However, it would be
  21514. > more cryptic to someone just getting into the language. Registers were
  21515. > the main thing that made me quit learning Perl quickly.
  21516. >
  21517.  
  21518. I would NOT want to PERL-ify Python ( at least in THAT sense )! 
  21519. I agree that that sort of stuff is part of why Perl is hard to read and learn.
  21520.  
  21521. A long time ago, I asked Guido for some remarks on his sense of what role
  21522. Python was meant to fill. *IF* the role was to be largely an interactive 
  21523. command language, THEN brevity has a much higher weight as a virtue. 
  21524. Perl, IMHO, since it is less interactive than Python, fills that sort of
  21525. role more poorly, so I see even less excuse for that sort of abbreviation. 
  21526. [ We want an interactive language to have aliases and other abbreviations, 
  21527.   but once we start using an editor to enter larger scripts, then readability
  21528.   becomes more virtuous than conciseness. ( IMHO ) ]
  21529.  
  21530. >
  21531. > Another way to look at this is that since Python is an object-oriented
  21532. > language the for statement above is sending a message to `for' with 2
  21533. > arguments. Since objects have their own private data, it would seem
  21534. > that we could type serverloop_code.insert(for.current_pos(), line) at
  21535. > line X to access some data in the `for' object.
  21536. > What do you all think ?
  21537.  
  21538. Since Python is not a "pure" object-oriented language, there is no way to
  21539. send 'for' a message. But you COULD define an object that keeps count of 
  21540. the number of times it has been assigned to. It would have two instance 
  21541. variables: value and count; and a method that updates value and increments
  21542. count. 
  21543.  
  21544. - - Steve 
  21545.  
  21546. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  21547.  Steven D. Majewski        University of Virginia Physiology Dept.
  21548.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  21549.  Voice: (804)-982-0831/32    1300 Jefferson Park Avenue
  21550.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  21551.  
  21552. ------- End of forwarded message -------
  21553. 
  21554. 
  21555. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  21556.     id AA29148 (5.65b/2.10/CWI-Amsterdam); Tue, 14 Jul 1992 10:25:24 +0200
  21557. Received: by voorn.cwi.nl with SMTP; Tue, 14 Jul 1992 08:25:28 GMT
  21558. Message-Id: <9207140825.AA07595@voorn.cwi.nl>
  21559. To: python-list@cwi.nl
  21560. Subject: Re: Emacs/Python connection; and the future of Python
  21561. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  21562. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  21563. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  21564. Date: Tue, 14 Jul 1992 10:25:27 +0200
  21565. Sender: Guido.van.Rossum@cwi.nl
  21566.  
  21567. Thanks to Terence Brannon for his Emacs/Python connection.  I've tried
  21568. it a bit and it works, but I haven't got the imagination right now to
  21569. find a good use for it -- I'm sure others will.  (To save you some
  21570. trouble while hunting down the required package "comint.el": the
  21571. pointer to it in the source is wrong: it is in the directory "as-is",
  21572. not in "packages".)
  21573.  
  21574. Regarding the future direction of Python: in my own use and that of
  21575. colleagues I find that Python is used more as a replacement for C
  21576. programs than as an improved perl or sh; interactive use tends to be
  21577. fairly simple (e.g. testing code I've just written, or as a
  21578. calculator).  I couldn't do without interactive Python (nor with the
  21579. command line editing) but I wouldn't use it to replace the shell
  21580. either.  What do other people use interactive Python for?
  21581.  
  21582. I'm leaving today for a vacation until July 30, so don't expect the
  21583. quick answers from me to all sorts of questions you're used to...
  21584.  
  21585. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  21586. "Well ass a matter of fact it's *very* runny, sir"
  21587. 
  21588. 
  21589. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  21590.     id AA05707 (5.65b/2.10/CWI-Amsterdam); Tue, 14 Jul 1992 16:31:52 +0200
  21591. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa28682;
  21592.           14 Jul 92 10:24 EDT
  21593. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  21594.     id AA15115; Tue, 14 Jul 92 10:23:03 EDT
  21595. Date: Tue, 14 Jul 92 10:23:03 EDT
  21596. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  21597. Message-Id: <9207141423.AA15115@aemsun.med.Virginia.EDU>
  21598. To: python-list@cwi.nl
  21599. Subject: What do I use python for?  ( A testimonial! ) 
  21600.  
  21601.  
  21602. > To: python-list@cwi.nl
  21603.  
  21604. > From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  21605. > Regarding the future direction of Python: in my own use and that of
  21606. > colleagues I find that Python is used more as a replacement for C
  21607. > programs than as an improved perl or sh; interactive use tends to be
  21608. > fairly simple (e.g. testing code I've just written, or as a
  21609. > calculator).  I couldn't do without interactive Python (nor with the
  21610. > command line editing) but I wouldn't use it to replace the shell
  21611. > either.  What do other people use interactive Python for?
  21612.  
  21613. I use python for:
  21614.  
  21615. (1) Some of the usual sed/awk/perl type file munging. 
  21616.     For a while, perhaps, I was overdoing it at trying to use Python as
  21617.     a replacement: I was in the middle of a ~10 line Python program when
  21618.     I realized I could do the job with an awk one liner. On the other 
  21619.     hand, I often can't remember of get straight the proper escapes for
  21620.     sed, so I used python yesterday to convert all of the carriage-returns
  21621.     into line-feeds in a file. After '/015' and '/r' didn't work in sed,
  21622.     it was easier to type a couple extra lines in python than dig out 
  21623.     the manual. If I was more of a wizzard at those other tools, I might
  21624.     not use python, but python is easier to learn and remember, and 
  21625.     sometimes if I can't remember the name of a method, I can cheat and
  21626.     type something like "[].__methods__" or "dir(module)" and I don't
  21627.     have to dig out the manual. 
  21628.  
  21629. (2) System management "perl" type jobs. Generating disk usage reports, etc.
  21630.  
  21631. (3) Some iteractive shell type stuff where I'm not sure what I'm doing 
  21632.     when I start. I may want to recursivly build a list of files with 
  21633.     some defining criteria... and then when I discover that there are 
  21634.     "too many" , pare that list down by some other criterion , before 
  21635.     actually doing something with the list. 
  21636.  
  21637. (4) Before I started to use GnuPlot, I used to use Python to read in 
  21638.     data files ( spectra ) and do some calculations on them ( difference
  21639.     and normalizing ) and pipe them to "|plot |tek &". I had a 'plot' 
  21640.     routine that did a 'popen' and wrote out a list of numbers to the 
  21641.     pipe. With GnuPlot, I've just been putting the intermediate data 
  21642.     into a file. GnuPlot plots functions OR files BUT NOT functions OF 
  21643.     files, so I miss this. ( You can't say 'plot "data.1" - "data.2" ' 
  21644.     for instance. ) I haven't looked into GnuPlot, so I don't know if
  21645.     it would be easy to add, or if I should adapt my python/plot scheme
  21646.     to gnuplot. 
  21647.  
  21648. (5) As Guido said, above: as a replacement for C, or at it's best, where
  21649.     I probably wouldn't have written a C program at all, but used a 
  21650.     calculator or 'bc' and a pencil. Often, what I think is going to 
  21651.     be a small job ends up getting more complicated.
  21652.  
  21653.     Example: We got a new detector for our X-ray spectrometer, and since
  21654.     the absorption characteristics was radically different from the 
  21655.     previous ones, we had to rebuild our least-squares-fit elemental
  21656.     references from scratch. ( Something I had never done and no one
  21657.     else remembered doing - I used to go to our Physicist and get some
  21658.     "magic numbers" to plug in - now all I had was one of his papers. ) 
  21659.     Part of the process involved peak ratio's ( weighted by atom ratio's) 
  21660.     of "binary" standards : KCl, NaCl, Na2SO4 - something I could have 
  21661.     done with a calculator, but I had the feeling that it probably wouldn't
  21662.     work right the first time, so I might as well write a Python program to
  21663.     do it. Well: I didn't work out the first time. There was obviously
  21664.     someting wrong with the data going in, and I was glad I wrote a 
  21665.     python program which I could extend to do some detective work: 
  21666.     The program calculated the ratio's, added the inverse ratio's to a 
  21667.     table, and given any one initial absolute value, it calculated the 
  21668.     values of all of the other elements recursively, sorted them by both
  21669.     value and element and printed them along with a "trace". Inconsistencies
  21670.     were easier to spot and the trace helped to show which sample was "bad". 
  21671.     ( If you started with Ca = 1.0, it generate values from all of the 
  21672.       Ca-to-something ratio's, then all of the something to something else, 
  21673.      etc. so you would end up with, say, 4 values for Mg, derrived from 
  21674.      different chains of ratios, and if the were not all nearly equal, 
  21675.      then some value up the chain was bad. ) The fact that I did not 
  21676.     have to deal with  any low level list-pointer processing made none
  21677.     of this daunting. ( Even though it had become a bigger job than I 
  21678.     initially thought, it STILL had to be done in a couple days! )
  21679.     
  21680.  
  21681.     Sorry if I bore you with details, but I just wanted to illustrate 
  21682.     how sometimes those "quickies" end up being bigger jobs that you
  21683.     expected. ( I'm sure everyone has there own experience of that sort. )
  21684.     And I found Python marvelous at that sort of thing. It was simple 
  21685.     to add the "audit trail" to my calculation after I found out that
  21686.     was needed. It didn't kill any other code. It would have been even 
  21687.     simpler if I had done it in a more object-oriented style. I wasn't
  21688.     that adept at Python when I started, so I used lists. 
  21689.  
  21690.  
  21691.     I'm currently working more on the data-acquisition end of things, 
  21692.     but if I ever get back to the data-PROCESSING part, an interactive 
  21693.     graphics & statistical package in python might be a good project.
  21694.  
  21695.     I haven't tried the socket library in python yet, but since current
  21696.     jobs involve some distributed processing, I will probably use python
  21697.     to interactively debug my applications with a prototype ("stub") server. 
  21698.  
  21699.  
  21700.     Much Thanks for a great tool, Guido! 
  21701.  
  21702.   - Steve 
  21703.  
  21704.  
  21705. ======== "If you have a hammer, find a nail" - George Bush,'91  =========
  21706.  Steven D. Majewski        University of Virginia Physiology Dept.
  21707.  sdm7g@Virginia.EDU        Box 449 Health Sciences Center
  21708.  Voice: (804)-982-0831/32    1300 Jefferson Park Avenue
  21709.  FAX:   (804)-982-1616        Charlottesville, VA 22908
  21710.  
  21711. 
  21712. 
  21713. Received: from hopscotch.ksr.com by charon.cwi.nl with SMTP
  21714.     id AA01342 (5.65b/2.10/CWI-Amsterdam); Wed, 15 Jul 1992 07:13:01 +0200
  21715. Received: from ksr.com (frankenstein.ksr.com) by hopscotch.ksr.com with SMTP
  21716.     id AA07563; Wed, 15 Jul 1992 01:11:56 -0400
  21717. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  21718.     id AA06450; Wed, 15 Jul 92 01:13:19 EDT
  21719. Received: by kaos.ksr.com (4.0/KSR-2.0)
  21720.     id AA26287; Wed, 15 Jul 92 01:13:17 EDT
  21721. Message-Id: <9207150513.AA26287@kaos.ksr.com>
  21722. To: python-list@cwi.nl
  21723. Subject: Re: Emacs/Python connection; and the future of Python
  21724. Date: Wed, 15 Jul 92 01:13:17 -0400
  21725. From: Tim Peters <tim@ksr.com>
  21726.  
  21727. >  ...
  21728. >  What do other people use interactive Python for?
  21729.  
  21730. Definitely not for a shell here either, Guido.  In the last week I used
  21731. an interactive Python (running in an Emacs py-mode buffer, of course
  21732. <grin>) for two new things:
  21733.  
  21734. 1) Investigating the effect of various cache-design choices on the speed
  21735.    of some memory-access patterns common in scientific programs.
  21736.  
  21737.    This is a kind of mathematical modeling through simulation.
  21738.    Interactivity is a major attraction when doing this kind of stuff
  21739.    ("hmm, sure didn't expect that!  let's change this parameter & see
  21740.    what happens then ...").  My 2nd choice for this would be ABC (which
  21741.    also would have done a fine job on it), but my fingers will never
  21742.    "know" the ABC editor as well as they know Emacs -- and running in an
  21743.    Emacs buffer is the ultimate "history" mode.
  21744.  
  21745. 2) Finding those integers N such that N*pi is closest to being exactly
  21746.    representable in the IEEE-754 64-bit format.
  21747.  
  21748.    Don't ask <grin -- it has to do with finding the worst cases for the
  21749.    argument-reduction phase of trig functions>.  This makes heavy
  21750.    demands on (very) large-integer arithmetic, and interactivity was
  21751.    useful mostly to run the program over a certain range, then
  21752.    progressively fiddle the routine to print less and less output as the
  21753.    numbers got larger & larger.  Interactivity was a convenience here,
  21754.    but not a necessity.
  21755.  
  21756. In both cases I'll echo something Steve M said:  these kinds of
  21757. exploratory applications tend to grow by leaps and bounds even while I'm
  21758. running them, and Python's design makes it amazingly easy to add new
  21759. code "on the fly" in a well-structured way (i.e., in a way that's not an
  21760. inherently incomprehensible mess the next day -- Python's clean module
  21761. structure, class gimmicks, and vrbls-are-local default all seem to play
  21762. a helpful part in this -- I'm not *exactly* sure how, though <0.6 grin>).
  21763.  
  21764. Unlike Steve, I never use Python for something that Perl can do in under
  21765. 10 lines -- I may be unique in never using Python for "system" stuff.
  21766.  
  21767. so-it's-a-testament-to-the-language-that-even-i<grin>-am-delighted-
  21768.    with-it-ly y'rs  - tim
  21769.  
  21770. Tim Peters   Kendall Square Research Corp
  21771. tim@ksr.com,         ksr!tim@uunet.uu.net
  21772. 
  21773. 
  21774. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  21775.     id AA04328 (5.65b/2.10/CWI-Amsterdam); Wed, 15 Jul 1992 10:25:51 +0200
  21776. Received: by schelvis.cwi.nl with SMTP; Wed, 15 Jul 1992 08:25:50 GMT
  21777. Message-Id: <9207150825.AA19674@schelvis.cwi.nl>
  21778. To: peter@mat.aukuni.ac.nz
  21779. Cc: python-list@cwi.nl
  21780. Subject: Re: sort() method 
  21781. In-Reply-To: Message by peter@mat.aukuni.ac.nz ,
  21782.          Wed, 15 Jul 92 17:31:56 NZST , <9207150531.AA00771@mat.aukuni.ac.nz> 
  21783. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  21784. Phone: +31 20 5924098(work), +31 20 6160335(home)
  21785. X-Last-Band-Seen: Mucky Pup (Melkweg), Giant Family (Korsakoff, 8-7)
  21786. X-Mini-Review: MP: energetic dancepunkmetal. GF: retrorock. both: good.
  21787. Date: Wed, 15 Jul 1992 10:25:49 +0200
  21788. From: Jack Jansen <Jack.Jansen@cwi.nl>
  21789.  
  21790. I don't think that having a built-in sort() with user-supplied compare
  21791. routine would be a big win when compared to programming sort yourself.
  21792. Calling out to the python code would probably loose you most of the
  21793. performance that programming in C would win you.
  21794.  
  21795. The only reasons I program some stuff I C are:
  21796. - features inaccessible from python (or difficult to access), like
  21797.   system calls or things involving hairy C structures, and
  21798. - manipulation of huge amounts of small data-items, like finding the
  21799.   average value of a sound sample (stored as byte values in a string).
  21800.  
  21801. The second case is why you want to program sort() in C, but if you
  21802. have to wrap each item as a python object and call out to a python
  21803. compare routine you loose the advantage.
  21804.  
  21805. A generic sort module in python should be peanuts, though. Anyone feel
  21806. like writing one?
  21807. --
  21808. Jack Jansen        | If I can't dance I don't want to be part of
  21809. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  21810. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  21811. 
  21812. 
  21813. Received: from ccvcom.aukuni.ac.nz by charon.cwi.nl with SMTP
  21814.     id AA01878 (5.65b/2.10/CWI-Amsterdam); Wed, 15 Jul 1992 07:54:04 +0200
  21815. Received: from mat.aukuni.ac.nz by aukuni.ac.nz; Wed, 15 Jul 92 17:32 +1200
  21816. Received: from matu12.mat.aukuni.ac.nz by mat.aukuni.ac.nz (4.1/SMI-4.1) id
  21817.  AA00771; Wed, 15 Jul 92 17:31:58 NZS
  21818. Date: Wed, 15 Jul 92 17:31:56 NZST
  21819. From: peter@mat.aukuni.ac.nz
  21820. Subject: sort() method
  21821. To: python-list@cwi.nl
  21822. Message-Id: <9207150531.AA00771@mat.aukuni.ac.nz>
  21823. X-Mailer: ELM [version 2.3 PL11]
  21824.  
  21825. I am a newcomer to Python and as an exercise, I am converting some of
  21826. my Perl scripts into Python. So far I am satisfied with the result and
  21827. haven't had serious difficulties. In particular I like the nice and
  21828. *readable* look of my new scripts.
  21829.  
  21830. During this exercise, I observed that the Python module system
  21831. encouraged me to reorganize my, sometimes not too nice, Perl scripts
  21832. into a better structured (modularized) form.
  21833.  
  21834. I haven't used object-oriented features yet.
  21835.  
  21836. However the speed of my Python scripts are far from of the perl ones.
  21837. It doesn't really worry me at the moment and anyway, it could be
  21838. a consequence of my beginner Python programming style.
  21839.  
  21840. One of the problems I've encountered is that I frequently used the Perl
  21841. feature which allows the user to give his own comparing function as a
  21842. parameter to the sort routine. I couldn't find anything similar in
  21843. Python and therefore the equivalent Python code segment became more
  21844. complicated.
  21845.  
  21846. Is there any Python style way to work around this problem?
  21847.  
  21848. If there was no such a way, wouldn't it be worth to add this extension
  21849. in some way to the builtin sort() method? (I hope it is not a "perlis"
  21850. wish :-)
  21851.  
  21852. --
  21853.            ,
  21854. Peter Dobcsanyi
  21855.  
  21856. 
  21857. 
  21858. Replied: Thu, 06 Aug 1992 20:20:22 +0200
  21859. Replied: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  21860. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  21861.     id AA26227 (5.65b/2.10/CWI-Amsterdam); Thu, 16 Jul 1992 00:07:53 +0200
  21862. Received: from aemsun.med.virginia.edu by uvaarpa.Virginia.EDU id aa25802;
  21863.           15 Jul 92 18:07 EDT
  21864. Received: by aemsun.med.Virginia.EDU (4.1/1.34)
  21865.     id AA18404; Wed, 15 Jul 92 18:05:34 EDT
  21866. Date: Wed, 15 Jul 92 18:05:34 EDT
  21867. From: "Steven D. Majewski" <sdm7g@aemsun.med.Virginia.EDU>
  21868. Message-Id: <9207152205.AA18404@aemsun.med.Virginia.EDU>
  21869. To: python-list@cwi.nl
  21870. Subject: Mac Python
  21871.  
  21872.  
  21873. I never got much of a chance to work on PC-python - 
  21874. I got switched over to some Mac programming jobs. 
  21875.  
  21876. I assume that the Mac python doesn't support the Sockets module (True?). 
  21877.  
  21878. I just managed to get the NCSA Sockect Library for MacTCP to compile
  21879. and build. ( For my current project. As soon as I have some free time
  21880. from that... ) I would like to re-build Mac Python with Sockets. 
  21881.  
  21882. I have the Mac Python application that someone built previously.
  21883.  
  21884. Does anyone have a Makefile for MPW? ( or was Think C used? ) 
  21885.  
  21886. Any problems, tips, or changes known to compile on Mac. 
  21887.  
  21888. Thanks, 
  21889.  - Steve Majewski 
  21890.  
  21891. 
  21892. 
  21893. Received: from hopscotch.ksr.com by charon.cwi.nl with SMTP
  21894.     id AA01741 (5.65b/2.10/CWI-Amsterdam); Thu, 16 Jul 1992 01:02:09 +0200
  21895. Received: from ksr.com (frankenstein.ksr.com) by hopscotch.ksr.com with SMTP
  21896.     id AA17083; Wed, 15 Jul 1992 19:01:01 -0400
  21897. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  21898.     id AA12498; Wed, 15 Jul 92 19:02:26 EDT
  21899. Received: by kaos.ksr.com (4.0/KSR-2.0)
  21900.     id AA05142; Wed, 15 Jul 92 19:02:23 EDT
  21901. Message-Id: <9207152302.AA05142@kaos.ksr.com>
  21902. To: python-list@cwi.nl
  21903. Subject: Re: sort() method
  21904. Date: Wed, 15 Jul 92 19:02:22 -0400
  21905. From: Tim Peters <tim@ksr.com>
  21906.  
  21907. >  ...
  21908. >  However the speed of my Python scripts are far from of the perl ones.
  21909.  
  21910. Depends on what you're doing ... e.g., at present, Perl is incomparably
  21911. faster at string searching, Python is incomparably faster at big-integer
  21912. arithmetic ...
  21913.  
  21914. >  ...
  21915. >  One of the problems I've encountered is that I frequently used the Perl
  21916. >  feature which allows the user to give his own comparing function as a
  21917. >  parameter to the sort routine. I couldn't find anything similar in
  21918. >  Python and therefore the equivalent Python code segment became more
  21919. >  complicated.
  21920. >
  21921. >  Is there any Python style way to work around this problem?
  21922.  
  21923. Note that Python does lexicographic sorting on tuples -- that is, the
  21924. first element of a tuple is used as the primary key, the second element
  21925. is the secondary key, and so on.  So, e.g.,
  21926.  
  21927. >>> a = [ ('a',9), ('b',2), ('aa',3), ('b',0) ]
  21928. >>> a.sort()
  21929. >>> a
  21930. [('a', 9), ('aa', 3), ('b', 0), ('b', 2)]
  21931. >>> 
  21932.  
  21933. Note that the list is sorted by the first tuple component (the string),
  21934. and that "ties" are resolved by sorting on the second tuple component
  21935. (the integer -- that's why the two "('b',...)" entries were swapped).
  21936.  
  21937. Lexicographic sorting is powerful, so the usual trick in Python is to
  21938. create your data this way to begin with, or to create a temp list,
  21939. embedding your data in tuples of which the first component is of a
  21940. simple type (int or string) and whose values reflect the ordering you
  21941. want.
  21942.  
  21943. But that is clumsy, if you need to do it often.  If, e.g., I want to
  21944. sort the list above by the 2nd tuple components instead, I usually do
  21945. the more Perl-like
  21946.  
  21947. >>> def cmp_2nd_num( (dummy1, n1), (dummy2, n2) ) : return n1 - n2
  21948. ...
  21949. >>> sort_by_2nd_num = make_sorter( cmp_2nd_num )
  21950. >>> sort_by_2nd_num( a )
  21951. [('b', 0), ('b', 2), ('aa', 3), ('a', 9)]
  21952. >>>
  21953.  
  21954. "make_sorter" is a function that takes a (Perl-like) comparison function
  21955. as argument, and returns a sorting function that sorts a list according
  21956. to the comparison function.  I'll attach Python code for "make_sorter"
  21957. below; it's not intended to be industrial-strength (to the contrary, it
  21958. can be very wasteful of both time & space); I simply use this way for
  21959. convenience.  Replace it with something smarter ...
  21960.  
  21961. >  ...
  21962. >  If there was no such a way, wouldn't it be worth to add this extension
  21963. >  in some way to the builtin sort() method?
  21964.  
  21965. Well, if you're running Python then I'd guess you have the source code
  21966. for it:  it is "worth it" to *you* to add this extension <grin>?
  21967.  
  21968. master-of-unfair-questions-ly y'rs  - tim
  21969.  
  21970. Tim Peters   Kendall Square Research Corp
  21971. tim@ksr.com,         ksr!tim@uunet.uu.net
  21972.  
  21973. Following is one way to code up the `make_sorter' function used above:
  21974.  
  21975. class Sorter:
  21976.     def init( self, cmp ):
  21977.     self.Cmp = cmp
  21978.     return self
  21979.  
  21980.     def sort( self, list ):
  21981.     # simple quicksort
  21982.     if len(list) <= 1: return list
  21983.  
  21984.     key = list[0]
  21985.     less, same, greater = [], [], []
  21986.     for thing in list:
  21987.         outcome = self.Cmp( thing, key )
  21988.         if   outcome < 0: which = less
  21989.         elif outcome > 0: which = greater
  21990.         else:          which = same
  21991.         which.append( thing )
  21992.  
  21993.     return self.sort(less) + same + self.sort(greater)
  21994.  
  21995. def make_sorter( cmp ):
  21996.     # return a sorting function that sorts a list according to comparator
  21997.     # function `cmp'
  21998.     # cmp(a,b) should return < 0 if a < b
  21999.     #                 = 0 if a = b
  22000.     #                 > 0 if a > b
  22001.     return Sorter().init( cmp ).sort
  22002.  
  22003. >>> END OF MSG
  22004. 
  22005. 
  22006. Replied: Thu, 30 Jul 1992 21:38:32 +0200
  22007. Replied: "python-list@cwi.nl "
  22008. Received: from ccvcom.aukuni.ac.nz by charon.cwi.nl with SMTP
  22009.     id AA09598 (5.65b/2.10/CWI-Amsterdam); Thu, 16 Jul 1992 07:07:58 +0200
  22010. Received: from mat.aukuni.ac.nz (matu1.mat.aukuni.ac.nz) by aukuni.ac.nz (PMDF
  22011.  #2864 ) id <01GMGBRRHYDC94DNY6@aukuni.ac.nz>; Thu, 16 Jul 1992 16:42:27 +1200
  22012. Received: from matu12.mat.aukuni.ac.nz by mat.aukuni.ac.nz (4.1/SMI-4.1) id
  22013.  AA02219; Thu, 16 Jul 92 14:16:42 NZS
  22014. Date: 16 Jul 1992 14:16:40 +1200 (NZST)
  22015. From: peter@mat.aukuni.ac.nz (Peter Dobcsanyi)
  22016. Subject: Re: sort() method
  22017. In-Reply-To: <9207150825.AA19674@schelvis.cwi.nl>; from "Jack Jansen" at Jul
  22018.  15, 92 10:25 am
  22019. To: python-list@cwi.nl
  22020. Message-Id: <9207160216.AA02219@mat.aukuni.ac.nz>
  22021. X-Envelope-To: python-list@cwi.nl
  22022. Content-Transfer-Encoding: 7BIT
  22023. X-Mailer: ELM [version 2.3 PL11]
  22024.  
  22025. Jack Jansen writes:
  22026. > I don't think that having a built-in sort() with user-supplied compare
  22027. > routine would be a big win when compared to programming sort yourself.
  22028. > Calling out to the python code would probably loose you most of the
  22029. > performance that programming in C would win you.
  22030.  
  22031. To be more precise, I did not think of writing any external C (or
  22032. whatever else other language) routine. What I meant is something like
  22033. that:
  22034.  
  22035.     def my_compare(x,y)
  22036.     if x <my_less> y    : return -1
  22037.     if x <my_equal> y    : return 0
  22038.     if x <my_greater> y    : return 1
  22039.  
  22040.     ......
  22041.     # then I could say
  22042.  
  22043.     foo_list.sort(my_compare)
  22044.  
  22045.  
  22046. > A generic sort module in python should be peanuts, though. Anyone feel
  22047. > like writing one?
  22048.  
  22049. Maybe this is the solution, but (being a beginner) I cannot see how can
  22050. one refine the *built-in* sort() method on Python level.
  22051.  
  22052. Could anyone, please, give some general idea about the mentioned
  22053. "peanuts".
  22054.  
  22055. -- 
  22056.            ,
  22057. Peter Dobcsanyi
  22058. 
  22059. 
  22060. Received: from uu.psi.com by charon.cwi.nl with SMTP
  22061.     id AA23891 (5.65b/2.10/CWI-Amsterdam); Fri, 17 Jul 1992 17:49:46 +0200
  22062. Received: from midas.UUCP by uu.psi.com (5.65b/4.1.031792-PSI/PSINet)
  22063.     id AA22568; Fri, 17 Jul 92 11:05:51 -0400
  22064. Received: from lynchpin.mtkgc.com by mtkgc.COM (4.1/SMI-4.1)
  22065.     id AA18091; Fri, 17 Jul 92 09:43:54 EDT
  22066. Received: by lynchpin.mtkgc.com (4.1/SMI-4.1)
  22067.     id AA07027; Fri, 17 Jul 92 09:48:15 EDT
  22068. Date: Fri, 17 Jul 92 09:48:15 EDT
  22069. From: samborn@lynchpin.mtkgc.COM (Kevin Samborn)
  22070. Message-Id: <9207171348.AA07027@lynchpin.mtkgc.com>
  22071. To: python-list@cwi.nl
  22072. Subject: Re: What do I use python for?  ( A testimonial! ) 
  22073.  
  22074. I this is what have used Python for:
  22075.  
  22076. A dialog manager for a user interface management tool (with Motif).
  22077. Python was inserted at the point that XtCallbacks were called, and the
  22078. python code was executed instead...
  22079.  
  22080. I wrote simple interfaces to the Xt widgets, and was able to do quite
  22081. a bit, including calling the original callbacks if necessary.
  22082.  
  22083. Now I am considering doing the same thing for OpenLook (OLIT).
  22084.  
  22085. kevin samborn
  22086. --
  22087. kevin samborn
  22088. samborn@mtkgc.com
  22089. 
  22090. 
  22091. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  22092.     id AA16550 (5.65b/2.10/CWI-Amsterdam); Mon, 20 Jul 1992 18:49:11 +0200
  22093. Received: from stun4b.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  22094.     id AA13659; Mon, 20 Jul 92 09:46:53 PDT
  22095. Date: Mon, 20 Jul 92 09:46:53 PDT
  22096. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  22097. Message-Id: <9207201646.AA13659@jove.cs.caltech.edu>
  22098. Received: by stun4b.cs.caltech.edu (4.1/SMI-4.1)
  22099.     id AA05788; Mon, 20 Jul 92 09:49:10 PDT
  22100. To: python-list@cwi.nl
  22101. Cc: tb06@pl118a.cc.lehigh.edu
  22102. Subject: graphics question: how to get the text of a TextEdit widget
  22103. Comments: Hyperbole mail buttons accepted, v3.02P.
  22104.  
  22105. I was able to create a TextEdit widget and put text in it by:
  22106.  
  22107. t = TextEdit.TextEdit().createboxed(group, (3,1),(2,2))
  22108. t.settext('howdy ho')
  22109.  
  22110. but t.text yields no string
  22111.  
  22112. any ideas why ?
  22113.  
  22114. --
  22115. terrence brannon
  22116. brannon@jove.cs.caltech.edu 
  22117.  
  22118. 
  22119. 
  22120. Replied: Thu, 30 Jul 1992 21:49:28 +0200
  22121. Replied: "brannon@jove.cs.caltech.edu (Terrence M. Brannon) "
  22122. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  22123.     id AA14158 (5.65b/2.10/CWI-Amsterdam); Tue, 28 Jul 1992 19:05:34 +0200
  22124. Received: from stun4q.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  22125.     id AA06971; Tue, 28 Jul 92 10:02:15 PDT
  22126. Date: Tue, 28 Jul 92 10:02:15 PDT
  22127. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  22128. Message-Id: <9207281702.AA06971@jove.cs.caltech.edu>
  22129. Received: by stun4q.cs.caltech.edu (4.1/SMI-4.1)
  22130.     id AA17925; Tue, 28 Jul 92 10:05:31 PDT
  22131. To: python-list@cwi.nl
  22132. Cc: brannon@jove.cs.caltech.edu
  22133. Subject: repeated C-c will cause a window to fail to close
  22134. Comments: Hyperbole mail buttons accepted, v3.02P.
  22135. Note: C-c means control-c
  22136.  
  22137. Has anyone created a window in the stdwin module of python and not
  22138. been able to get it to close because you sent a series of C-c at
  22139. the Python interpreter?
  22140.  
  22141. I am using C-c from Emacs as a signal to the python interpreter that
  22142. emacs has finished what it has requested like so:
  22143.  
  22144.  
  22145.  
  22146.     python interp -- calls emacs lisp----> emacs does function ---|
  22147.                                        |
  22148.                      python     <------    sends C-c<------
  22149.                     continues    python interp
  22150.                             process to signal
  22151.                             completion
  22152.  
  22153. Python does its wait by doing a try:
  22154.                     emacs.dired('.')
  22155.                 except:
  22156.                     print 'emacs is finished'
  22157.  
  22158. --
  22159. terrence brannon
  22160. brannon@jove.cs.caltech.edu 
  22161.  
  22162. 
  22163. 
  22164. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  22165.     id AA12486 (5.65b/2.10/CWI-Amsterdam); Wed, 29 Jul 1992 10:30:42 +0200
  22166. Received: by schelvis.cwi.nl with SMTP; Wed, 29 Jul 1992 08:30:31 GMT
  22167. Message-Id: <9207290830.AA10683@schelvis.cwi.nl>
  22168. To: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  22169. Cc: python-list@cwi.nl
  22170. Subject: Re: repeated C-c will cause a window to fail to close 
  22171. In-Reply-To: Message by brannon@jove.cs.caltech.edu (Terrence M. Brannon) ,
  22172.          Tue, 28 Jul 92 10:02:15 PDT , <9207281702.AA06971@jove.cs.caltech.edu> 
  22173. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  22174. Phone: +31 20 5924098(work), +31 20 6160335(home)
  22175. X-Last-Band-Seen: Some have Fins (Paradiso, 25-7)
  22176. X-Mini-Review: I like loud bands, but this was ridiculous. Fled after 5 mins.
  22177. Date: Wed, 29 Jul 1992 10:30:30 +0200
  22178. From: Jack Jansen <Jack.Jansen@cwi.nl>
  22179.  
  22180. Terrence,
  22181. I don't fully understand what you mean. The stdwin window doesn't
  22182. react to the 'close' menu? Or doesn't it react to deleting the object?
  22183.  
  22184. Hmm.... I'm just think of something, wait a minute... Yes! There is
  22185. some sort of cyclic reference in stdwin that occasionally causes
  22186. windows to stay around when you think they are deleted. For that
  22187. reason, there is also an explicit 'close()' method on stdwin windows.
  22188. Maybe this is what you're looking for (although it has little to do
  22189. with emacs and ^C)?
  22190. --
  22191. Jack Jansen        | If I can't dance I don't want to be part of
  22192. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  22193. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  22194. 
  22195. 
  22196. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  22197.     id AA07119 (5.65b/2.10/CWI-Amsterdam); Thu, 30 Jul 1992 01:56:32 +0200
  22198. Received: from stun4q.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  22199.     id AA11219; Wed, 29 Jul 92 16:52:22 PDT
  22200. Date: Wed, 29 Jul 92 16:52:22 PDT
  22201. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  22202. Message-Id: <9207292352.AA11219@jove.cs.caltech.edu>
  22203. Received: by stun4q.cs.caltech.edu (4.1/SMI-4.1)
  22204.     id AA18522; Wed, 29 Jul 92 16:55:45 PDT
  22205. To: python-list@cwi.nl
  22206. Subject: Something TextEdit.py needs
  22207. Comments: Hyperbole mail buttons accepted, v3.02P.
  22208.  
  22209.  
  22210. it has a settext method but no gettext method . 
  22211.  
  22212. i added one myself because i needed it . here it is :
  22213.  
  22214. def gettext(self):
  22215.     self.editor.gettext()
  22216.  
  22217.  
  22218. love ,
  22219.  
  22220. Terrence Brannon -- brannon@jove.cs.caltech.edu
  22221.  
  22222. 
  22223. 
  22224. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22225.     id AA00149 (5.65b/2.10/CWI-Amsterdam); Thu, 30 Jul 1992 21:38:29 +0200
  22226. Received: by voorn.cwi.nl with SMTP; Thu, 30 Jul 1992 19:38:34 GMT
  22227. Message-Id: <9207301938.AA03547@voorn.cwi.nl>
  22228. To: python-list@cwi.nl
  22229. Subject: Re: sort() method 
  22230. In-Reply-To: Your message of "16 Jul 1992 14:16:40 MDT."
  22231.              <9207160216.AA02219@mat.aukuni.ac.nz> 
  22232. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22233. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22234. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22235. Date: Thu, 30 Jul 1992 21:38:33 +0200
  22236. Sender: Guido.van.Rossum@cwi.nl
  22237.  
  22238. Here are some comments on the issue of a proposed extension to
  22239. list.sort() (an optional comparison function that determines how the
  22240. elements of a list are to be compared).
  22241.  
  22242. There's nothing wrong with this wish, and it has come up before
  22243. (although I wouldn't call it perl-ish but C-ish, see qsort(3)).
  22244.  
  22245. The value of such an extension, however, is limited, and usually
  22246. the alternative method suggested by Tim Peters is better: create a
  22247. temporary list containing tuples containing some function of the items
  22248. to be compared that determines the right ordering.  E.g., to sort
  22249. strings independent of case, create a list containing tuples with the
  22250. string converted to lowercase first and the original string second,
  22251. and sort this list.
  22252.  
  22253. I'll try to explain why I think this way of sorting is better in many
  22254. cases.
  22255.  
  22256. Doing the equivalent in C would be wasteful of space and probably not
  22257. significantly faster than a cleverly-coded version that does the
  22258. lowercase conversion on the fly.  However, the situation in Python is
  22259. different.  True, we are wasting some space, but we are gaining speed,
  22260. since a "cleverly-coded" comparison function in Python has to be much
  22261. slower than Python's built-in tuple or string comparison operation.
  22262. (In general, many techniques for "clever coding" in C don't work in
  22263. Python because the relative speed of operations are vastly different.)
  22264.  
  22265. Knuth tells us that sorting takes at least, roughly, N log2 N
  22266. comparisons.  Using the "comparison-function" method to sort a
  22267. 1000-item list would mean roughly 10000 calls to this function, and
  22268. thus 20000 calls to string.lower().  On the other hand, the "temp
  22269. list" method would require only 1000 calls!
  22270.  
  22271. There are, however, situations in which the ordering of items is not
  22272. easily converted to an equivalent ordering on some function of the
  22273. items, while comparing any two items would be a piece of cake.  A
  22274. specia case, not unimportant for Python, is the situation of a
  22275. user-defined class whose objects have a cmp() method.
  22276.  
  22277. It is easy to add an optional comparison function to list.sort() now,
  22278. so that I might just do that.  (Maybe it wasn't so easy when I wrote
  22279. the original code; calling Python back from C is a "recent" addition
  22280. to the tools available to the extension writer.)
  22281.  
  22282. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22283. "Oh, so you want to fly an AEROPLANE?"
  22284. 
  22285. 
  22286. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22287.     id AA00658 (5.65b/2.10/CWI-Amsterdam); Thu, 30 Jul 1992 21:49:59 +0200
  22288. Received: by voorn.cwi.nl with SMTP; Thu, 30 Jul 1992 19:50:02 GMT
  22289. Message-Id: <9207301950.AA03572@voorn.cwi.nl>
  22290. To: python-list@cwi.nl
  22291. Subject: Re: repeated C-c will cause a window to fail to close 
  22292. In-Reply-To: Your message of "Tue, 28 Jul 1992 10:02:15 MDT."
  22293.              <9207281702.AA06971@jove.cs.caltech.edu> 
  22294. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22295. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22296. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22297. Date: Thu, 30 Jul 1992 21:50:01 +0200
  22298. Sender: Guido.van.Rossum@cwi.nl
  22299.  
  22300. >Has anyone created a window in the stdwin module of python and not
  22301. >been able to get it to close because you sent a series of C-c at
  22302. >the Python interpreter?
  22303.  
  22304. There's a problem with Python's STDWIN interface and C-c that might
  22305. explain your problem.  When the Python code calls stdwin.getevent(),
  22306. interrupt signals sent to the Python interpreter are postponed until
  22307. this function returns an event, and then not always handled
  22308. adequately.  Unfortunately, it would require a significant redesign of
  22309. the underlying STDWIN library and interface to fix this (simply
  22310. long-jumping out of the underlying wgetevent() call might leave the
  22311. internal administration of STDWIN or of the under-under-lying X11 or
  22312. Mac window system in a bad state).
  22313.  
  22314. I suggest that you redesign your code to avoid using signals (that's
  22315. better anyway, for various reasons), e.g. the Emacs code could always
  22316. send a line of input to the Python process when it is finished, and
  22317. the Python interface could wait for this line.
  22318.  
  22319. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22320. "May your mailbox overflow with messages" (modern Chinese curse)
  22321. 
  22322. 
  22323. Replied: Sat, 01 Aug 1992 01:54:15 +0200
  22324. Replied: python-list@cwi.nl
  22325. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  22326.     id AA13593 (5.65b/2.10/CWI-Amsterdam); Fri, 31 Jul 1992 21:16:35 +0200
  22327. Received: from stun4r.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  22328.     id AA14940; Fri, 31 Jul 92 12:13:00 PDT
  22329. Date: Fri, 31 Jul 92 12:13:00 PDT
  22330. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  22331. Message-Id: <9207311913.AA14940@jove.cs.caltech.edu>
  22332. Received: by stun4r.cs.caltech.edu (4.1/SMI-4.1)
  22333.     id AA07442; Fri, 31 Jul 92 12:16:38 PDT
  22334. To: python-list@cwi.nl
  22335. Subject: unexpect behavior from my added method
  22336. Comments: Hyperbole mail buttons accepted, v3.02P.
  22337.  
  22338. I need to read the value that the user inputs into a TextEdit widget.
  22339. lib/TextEdit.py in the Python distribution does not have a gettext()
  22340. method only a settext() method, so I wrote the following:
  22341.  
  22342. def gettext(self):
  22343.     self.editor.gettext()
  22344.  
  22345. However instead of just getting the text of the TextEdit widget, I get
  22346. the word None also. IN other words if the user has input as 12 into
  22347. the TextEdit widget and afterwards I do this:
  22348.  
  22349. tmp = [text_edit_widget.gettext()]
  22350. print tmp[0]
  22351.  
  22352. the output is:
  22353.  
  22354. '12'
  22355. None
  22356.  
  22357.  
  22358.  
  22359. Instead of just '12' as I would think that it should be 
  22360.  
  22361. love ,
  22362.  
  22363. Terrence Brannon -- brannon@jove.cs.caltech.edu
  22364.  
  22365. 
  22366. 
  22367. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22368.     id AA28633 (5.65b/2.10/CWI-Amsterdam); Sat, 1 Aug 1992 01:54:08 +0200
  22369. Received: by voorn.cwi.nl with SMTP; Fri, 31 Jul 1992 23:54:14 GMT
  22370. Message-Id: <9207312354.AA06430@voorn.cwi.nl>
  22371. To: python-list@cwi.nl
  22372. Subject: Re: unexpect behavior from my added method 
  22373. In-Reply-To: Your message of "Fri, 31 Jul 1992 12:13:00 MDT."
  22374.              <9207311913.AA14940@jove.cs.caltech.edu> 
  22375. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22376. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22377. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22378. Date: Sat, 01 Aug 1992 01:54:14 +0200
  22379. Sender: Guido.van.Rossum@cwi.nl
  22380.  
  22381. Terence Brannon writes:
  22382.  
  22383. >def gettext(self):
  22384. >    self.editor.gettext()
  22385.  
  22386. and wonders why it doesn't work (it prints the value but returns
  22387. None).
  22388.  
  22389. Try this instead:
  22390.  
  22391. def gettext(self):
  22392.     return self.editor.gettext()
  22393.  
  22394. This probably belongs in the FAQ: any expression that doesn't evaluate
  22395. to "None" is printed when executed as a statement.  This is useful
  22396. when using Python as an interactive calculator.  I left it in for
  22397. non-interactive statements: partly to simplify the interpreter (it's
  22398. not always easy to figure out whether a statement is executed
  22399. interactively; partly for consistency (a statement's effect is the
  22400. same interactively or in a function); partly to force programmers to
  22401. think about what to do with function return values.  If you don't want
  22402. to use a value returned by a function you have to assign it to a dummy
  22403. variable.  If you want to return it, you must use a return statement.
  22404.  
  22405. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22406. "I don't care how excrementally runny it is, hand it over with all speed!"
  22407. 
  22408. 
  22409. Replied: Tue, 04 Aug 1992 10:59:13 +0200
  22410. Replied: "python-list@cwi.nl (Python Mailing List) "
  22411. Received: from ccvcom.aukuni.ac.nz by charon.cwi.nl with SMTP
  22412.     id AA07526 (5.65b/2.10/CWI-Amsterdam); Tue, 4 Aug 1992 10:25:42 +0200
  22413. Received: from mat.aukuni.ac.nz (matu1.mat.aukuni.ac.nz) by aukuni.ac.nz (PMDF
  22414.  #2864 ) id <01GN6JJ39EV494DYG6@aukuni.ac.nz>; Tue, 4 Aug 1992 11:05:08 +1200
  22415. Received: from matu12.mat.aukuni.ac.nz by mat.aukuni.ac.nz (4.1/SMI-4.1) id
  22416.  AA00933; Tue, 4 Aug 92 11:04:34 NZS
  22417. Date: 04 Aug 1992 11:04:32 +1200 (NZST)
  22418. From: peter@mat.aukuni.ac.nz (Peter Dobcsanyi)
  22419. Subject: Bug!?
  22420. To: python-list@cwi.nl (Python Mailing List)
  22421. Message-Id: <9208032304.AA00933@mat.aukuni.ac.nz>
  22422. Content-Transfer-Encoding: 7BIT
  22423. X-Mailer: ELM [version 2.3 PL11]
  22424.  
  22425. I accidentally found the following strange behavior:
  22426.  
  22427. PYTHON SCRIPT:
  22428.  
  22429.     #!/usr/local/bin/python
  22430.     #
  22431.  
  22432.     import string
  22433.  
  22434.     x = string.atoi('01')
  22435.     print x
  22436.  
  22437.     x = string.atoi('07')
  22438.     print x
  22439.  
  22440.     x = string.atoi('08')
  22441.     print x
  22442.  
  22443.  
  22444. RESULT:
  22445.  
  22446. 1
  22447. 7
  22448. SyntaxError: invalid syntax
  22449. Stack backtrace (innermost last):
  22450.   File "p", line 12
  22451.     x = string.atoi('08')
  22452.   File "/usr/local/lib/python/string.py", line 110
  22453.     return eval(str)
  22454.  
  22455.  
  22456. INTERACTIVE:
  22457.  
  22458.     % python
  22459.     Python 0.9.6 (>= 1 Jan 1992).
  22460.     Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  22461.     >>> import string
  22462.     >>> x = string.atoi('07')
  22463.     >>>
  22464.  
  22465.     Python 0.9.6 (>= 1 Jan 1992).
  22466.     Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  22467.     >>> import string
  22468.     >>> x = string.atoi('08')
  22469.     SyntaxError: invalid syntax
  22470.     Stack backtrace (innermost last):
  22471.       File "<stdin>", line 1
  22472.       File "/usr/local/lib/python/string.py", line 110
  22473.     return eval(str)
  22474.     >>>
  22475.  
  22476. --
  22477.            ,
  22478. Peter Dobcsanyi
  22479. 
  22480. 
  22481. To: python-list@cwi.nl (Python Mailing List)
  22482. Subject: Re: Bug!? 
  22483. In-reply-to: Your message of "04 Aug 1992 11:04:32 MDT."
  22484.              <9208032304.AA00933@mat.aukuni.ac.nz> 
  22485. From: Guido van Rossum <guido@cwi.nl>
  22486. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22487. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22488. Date: Tue, 04 Aug 1992 10:59:14 +0200
  22489. Sender: guido
  22490.  
  22491. Peter Dobcsanyi writes:
  22492. >I accidentally found the following strange behavior:
  22493. [...]
  22494. >    x = string.atoi('08')
  22495. [...]
  22496. >SyntaxError: invalid syntax
  22497.  
  22498. Yeah, string.atoi() internally calls eval(), which interprets this as
  22499. an illegal octal number.  According to the documentation string.atoi()
  22500. is not supposed to handle octal numbers, so I'll fix it.  (Would this
  22501. break anybody's code?)
  22502.  
  22503. Here is what I believe is a fixed version of string.atoi():
  22504.  
  22505. # Convert string to integer
  22506. atoi_error = 'non-numeric argument to string.atoi'
  22507. def atoi(str):
  22508.     sign = ''
  22509.     s = str
  22510.     if s[:1] in '+-':
  22511.         sign = s[0]
  22512.         s = s[1:]
  22513.     if not s: raise atoi_error, str
  22514.     while s[0] == '0' and len(s) > 1: s = s[1:]
  22515.     for c in s:
  22516.         if c not in digits: raise atoi_error, str
  22517.     return eval(sign + s)
  22518.  
  22519. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22520. "Are all your pets called Eric?"
  22521. 
  22522. 
  22523. Received: from ben.uknet.ac.uk by charon.cwi.nl with SMTP
  22524.     id AA01550 (5.65b/2.10/CWI-Amsterdam); Tue, 4 Aug 1992 18:28:16 +0200
  22525. Received: from eros.uknet.ac.uk by ben.uknet.ac.uk via UKIP with SMTP (PP) 
  22526.           id <sg.11597-0@ben.uknet.ac.uk>; Tue, 4 Aug 1992 16:16:52 +0100
  22527. Received: from camscan.co.uk by eros.uknet.ac.uk with UUCP 
  22528.           id <6130-0@eros.uknet.ac.uk>; Tue, 4 Aug 1992 16:16:47 +0100
  22529. Received: from electra.camscan.co.uk by star.camscan.co.uk;
  22530.           Tue, 4 Aug 92 15:33:13 BST
  22531. From: Gerry Stringer <gerry@camscan.co.uk>
  22532. Date: Tue, 4 Aug 92 15:33:16 BST
  22533. Message-Id: <6357.9208041433@electra.camscan.co.uk>
  22534. To: python-list@cwi.nl
  22535. Subject: interface generation
  22536.  
  22537.  
  22538.  
  22539. Hello,
  22540.  
  22541. Question: has anyone automated/semi-automated generation of Python
  22542. interface code (ideally for C++) before I think about having a go?
  22543.  
  22544. Waffle for your interest: my company's application involves collecting
  22545. X-ray data in the form of spectra from a box attached to an electron
  22546. microscope and then displaying it, analysing what elements it contains
  22547. etc.
  22548.  
  22549. We've actually designed the software as closely cooperating multiple
  22550. processes rather than some monolith eg. one process holds and displays
  22551. spectra while another controls acquiring it, another process analyses
  22552. it etc.
  22553.  
  22554. We write in C++ and have developed our own C++ library called RMC
  22555. (Remote Method Call) so that eg. object.dosomething(arg1,arg2) while
  22556. appearing to run locally, actually does the equivalent of RPC ie.
  22557. packaging args, executing code in another process, unpackaging
  22558. results.
  22559.  
  22560. To stop terminal boredom typing in code, we have "rmcgen" which takes a
  22561. classname, methods and their args and generates the C++ to interface to
  22562. RMC. It is this, or a Postgres preprocessor I developed, that I would
  22563. probably use as the starting point for Python interface generation. 
  22564.  
  22565. We have an OPENWINDOWS GUI front end to all this but we still need an
  22566. interpreter (for a different sort of translation?) to automate tasks at
  22567. a high level hence Python.
  22568.  
  22569. Many thanks again to Guido who advised me about how to have a single
  22570. common shared library of core Python code with process specific
  22571. modules.
  22572.  
  22573.         gerry
  22574.  
  22575. +--------------------------------+---------------------------------+
  22576. |Gerry Stringer                  |Phone: +44 954 780926            |
  22577. |Cambridge Scanning Company Ltd  |Fax:   +44 954 789829            |
  22578. |Saxon Way                       |UUCP : gerry@camscan.co.uk       |
  22579. |Bar Hill                        |       gerry@camscan.uucp        |
  22580. |Cambridge CB3 8SL               |                      |
  22581. |United Kingdom                  |                                 |
  22582. +--------------------------------+---------------------------------+
  22583. 
  22584. 
  22585. Replied: Tue, 04 Aug 1992 21:49:09 +0200
  22586. Replied: "python-list "
  22587. Replied: Tue, 04 Aug 1992 21:46:56 +0200
  22588. Replied: "Jaap Vermeulen <jaap@sequent.com> "
  22589. Received: from gateway.sequent.com by charon.cwi.nl with SMTP
  22590.     id AA04852 (5.65b/2.10/CWI-Amsterdam); Tue, 4 Aug 1992 21:31:03 +0200
  22591. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  22592.     id AA19901; Tue, 4 Aug 92 12:31:22 -0700
  22593. Received: by eng2.sequent.com (5.65/1.34)
  22594.     id AA23469; Tue, 4 Aug 92 12:28:28 -0700
  22595. Message-Id: <9208041928.AA23469@eng2.sequent.com>
  22596. To: Python Mailing List <python-list@cwi.nl>
  22597. Subject: How do I get to the local dictionary in a function?
  22598. Priority: Normal
  22599. Precedence: first-class
  22600. Organization: Sequent Computer Systems, Inc.
  22601.           Decision Support Group - MailStop: RHE2-358
  22602.           15450 S.W. Koll Parkway
  22603.           Beaverton, OR  97006
  22604. X-Phone: (503) 578-4404
  22605. X-Fax: (503) 578-7569
  22606. X-Uucp: ...!uunet!sequent!jaap
  22607. X-Internet: jaap@sequent.com
  22608. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  22609.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  22610.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  22611. Date: Tue, 04 Aug 92 12:28:27 PDT
  22612. From: Jaap Vermeulen <jaap@sequent.com>
  22613.  
  22614.  
  22615. I would like to pass along the local namespace (dictionary) of a function
  22616. into an exec() statement.  Either I'm overlooking something or it is
  22617. buried somewhere in the manual since I can't seem to find how to do that.
  22618. I can get to the global namespace, but not the local.
  22619.  
  22620. The problem is that I want to specify the global namespace explicitly.
  22621. This defaults to the local namespace being the global namespace, which
  22622. is exactly what I do *not* want.
  22623.  
  22624. Anybody any clues?
  22625.  
  22626. Example:
  22627.  
  22628.     >>> global_namespace = {}
  22629.  
  22630.     >>> def showit:
  22631.     ...     var_in_local_namespace = 0
  22632.     ...     # I wish I could give the local namespace as third argument
  22633.     ...     exec ('var_in_local_namespace = 1\n', global_namespace)
  22634.     ...     return var_in_local_namespace
  22635.  
  22636.     >>> showit()
  22637.     0
  22638.     >>>
  22639.  
  22640. Thanks,
  22641.  
  22642.     -Jaap-
  22643. --
  22644. Jaap Vermeulen                    +--------------------------+
  22645.                         | Sequent Computer Systems |
  22646.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  22647.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  22648. 
  22649. 
  22650. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22651.     id AA05243 (5.65b/2.10/CWI-Amsterdam); Tue, 4 Aug 1992 21:49:03 +0200
  22652. Received: by voorn.cwi.nl with SMTP; Tue, 4 Aug 1992 19:49:09 GMT
  22653. Message-Id: <9208041949.AA06949@voorn.cwi.nl>
  22654. To: python-list@cwi.nl
  22655. Subject: Re: How do I get to the local dictionary in a function? 
  22656. In-Reply-To: Your message of "Tue, 04 Aug 1992 12:28:27 MDT."
  22657.              <9208041928.AA23469@eng2.sequent.com> 
  22658. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22659. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22660. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22661. Date: Tue, 04 Aug 1992 21:49:09 +0200
  22662. Sender: Guido.van.Rossum@cwi.nl
  22663.  
  22664. Jaap Vermeulen writes:
  22665. >I would like to pass along the local namespace (dictionary) of a function
  22666. >into an exec() statement.  Either I'm overlooking something or it is
  22667. >buried somewhere in the manual since I can't seem to find how to do that.
  22668. >I can get to the global namespace, but not the local.
  22669.  
  22670. You cannot do that, and in a future version of Python the local
  22671. namespace of a function may not be implemented as a dictionary.
  22672. I played a little with implementing it as a mutable tuple, indexed by
  22673. variable number, and it makes an awful lot of difference in execution
  22674. speed.  (Unfortunately there are a lot of minor things to be cleared up
  22675. before this can be used in general, and it won't be in the next
  22676. release.)
  22677.  
  22678. Why do you the want to access local namespace?
  22679.  
  22680. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22681. "Do you want to have just one argument or were you thinking of taking
  22682. a course?"
  22683. [Sorry for the duplicate message Jaap, I must train my fingers :-) ]
  22684. 
  22685. 
  22686. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22687.     id AA16027 (5.65b/2.10/CWI-Amsterdam); Mon, 10 Aug 1992 23:50:08 +0200
  22688. Received: by voorn.cwi.nl with SMTP; Mon, 10 Aug 1992 21:50:14 GMT
  22689. Message-Id: <9208102150.AA14797@voorn.cwi.nl>
  22690. To: Jaap Vermeulen <jaap@sequent.com>
  22691. Cc: Python Mailing List <python-list@cwi.nl>
  22692. Subject: Re: Observations about dbm 
  22693. In-Reply-To: Your message of "Mon, 10 Aug 1992 09:32:30 MDT."
  22694.              <9208101632.AA09713@eng2.sequent.com> 
  22695. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22696. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22697. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22698. Date: Mon, 10 Aug 1992 23:50:13 +0200
  22699. Sender: Guido.van.Rossum@cwi.nl
  22700.  
  22701. >This one is for Jack: I was using the dbm module and am a little
  22702. >disappointed that it only takes strings as data (although I shouldn't look
  22703. >a gift horse in the mouth :-). I was wondering how much effort it would
  22704. >be to support all data types that are supported by the module 'marshal'?
  22705.  
  22706. Actually, this was done on purpose.  With the current set-up you can
  22707. share databases between C programs and Python code.  If you want to
  22708. store arbitrary Python values in a database, just place them between
  22709. `reverse quotes` when putting them in and pass them through eval()
  22710. after taking them out.
  22711.  
  22712. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22713. "Eric, being such a happy cat, was a piece of cake."
  22714. 
  22715. 
  22716. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22717.     id AA16036 (5.65b/2.10/CWI-Amsterdam); Mon, 10 Aug 1992 23:56:56 +0200
  22718. Received: by voorn.cwi.nl with SMTP; Mon, 10 Aug 1992 21:57:03 GMT
  22719. Message-Id: <9208102157.AA14815@voorn.cwi.nl>
  22720. To: Jaap Vermeulen <jaap@sequent.com>
  22721. Cc: Python Mailing List <python-list@cwi.nl>
  22722. Subject: Re: Observations about types
  22723. In-Reply-To: Your message of "Mon, 10 Aug 1992 09:32:30 MDT."
  22724.              <9208101632.AA09713@eng2.sequent.com> 
  22725. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22726. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22727. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22728. Date: Mon, 10 Aug 1992 23:57:02 +0200
  22729. Sender: Guido.van.Rossum@cwi.nl
  22730.  
  22731. >On another noter: I was trying to do some reasonable type checking in
  22732. >some routines I was writing for a stress test harnass. The problem is
  22733. >that type() only returns the generic type, not the specific type. I.e.
  22734. >if I have an instance of class 'foo', type(instance) will say <type
  22735. >'instance'>, but not <type 'instance foo'> or so.
  22736.  
  22737. True, but for class instances you can access the "secret" attribute
  22738. __class__ to get at the class.
  22739.  
  22740. For many other purposes of type checking you also need much more
  22741. information than what type() returns; e.g. in many situations tuples
  22742. of different length should be considered different types, and the
  22743. types of the items should be considered as well.  Luckily it isn't too
  22744. hard to write a function that returns a unique string with the
  22745. required properties -- type() is just a low-level tool to be used by
  22746. such a function.
  22747.  
  22748. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22749. "I told you.  We're an anarcho-syndicalist commune."
  22750. 
  22751. 
  22752. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  22753.     id AA08968 (5.65b/2.10/CWI-Amsterdam); Tue, 11 Aug 1992 11:46:29 +0200
  22754. Received: by schelvis.cwi.nl with SMTP; Tue, 11 Aug 1992 09:46:29 GMT
  22755. Message-Id: <9208110946.AA02940@schelvis.cwi.nl>
  22756. To: Jaap Vermeulen <jaap@sequent.com>
  22757. Cc: Python Mailing List <python-list@cwi.nl>
  22758. Subject: Re: Observations about instances, types and dbm 
  22759. In-Reply-To: Message by Jaap Vermeulen <jaap@sequent.com> ,
  22760.          Mon, 10 Aug 92 09:32:30 PDT , <9208101632.AA09713@eng2.sequent.com> 
  22761. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  22762. Phone: +31 20 5924098(work), +31 20 6160335(home)
  22763. X-Last-Band-Seen: Rythm Pigs (Melkweg, 5-8)
  22764. X-Mini-Review: Lame.
  22765. Date: Tue, 11 Aug 1992 11:46:28 +0200
  22766. From: Jack Jansen <Jack.Jansen@cwi.nl>
  22767.  
  22768.  
  22769. Recently, Jaap Vermeulen <jaap@sequent.com> said:
  22770.     
  22771.     This one is for Jack: I was using the dbm module and am a little
  22772.     disappointed that it only takes strings as data (although I shouldn't look
  22773.     a gift horse in the mouth :-). I was wondering how much effort it would
  22774.     be to support all data types that are supported by the module 'marshal'?
  22775.  
  22776. Hmm, I'm not too sure about this. An ideal solution would be if you
  22777. could write a wrapper in python to do this, but that is currently
  22778. impossible. Guido, would it be possible to extend python to allow you
  22779. to declare classes that mimic builtin types? I can envision a model
  22780. whereby the builtin class Mapping() defines methods get(index) and
  22781. put(index, value), and where I can then define a class that inherits
  22782. Mapping() and redefines get() and put(). The parser should then
  22783. translate 'foo[expr]' into 'foo.get(expr)' and that's that.
  22784.  
  22785. But then, I'm probably seeing things in a far too simple way...
  22786. --
  22787. Jack Jansen        | If I can't dance I don't want to be part of
  22788. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  22789. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  22790. 
  22791. 
  22792. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  22793.     id AA20623 (5.65b/2.10/CWI-Amsterdam); Thu, 13 Aug 1992 11:36:17 +0200
  22794. Received: by voorn.cwi.nl with SMTP; Thu, 13 Aug 1992 09:36:24 GMT
  22795. Message-Id: <9208130936.AA23745@voorn.cwi.nl>
  22796. To: python-list@cwi.nl
  22797. Subject: Re: Minor bug, perhaps? + (PC-Python/CS1) 
  22798. In-Reply-To: Your message of "13 Aug 1992 10:27:22 MET."
  22799.              <MAILQUEUE-101.920813102722.288@cs.ufh.ac.za> 
  22800. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  22801. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  22802. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  22803. Date: Thu, 13 Aug 1992 11:36:23 +0200
  22804. Sender: Guido.van.Rossum@cwi.nl
  22805.  
  22806. Stoffel,
  22807.  
  22808. Glad to hear your opinion about Python's "teachability"!
  22809.  
  22810. Regarding your problem I was afraid this would happen some day!
  22811.  
  22812. >Here we have an assignment statement following directly on a `one-line' if:
  22813. >        ...
  22814. >        if i==n: break
  22815. >        j = i
  22816. >        ...
  22817.  
  22818. Looking carefully at your example, there are 8 spaces before the "if",
  22819. but two tabs before the "j = i" line.  (I'm glad that e-mail doesn't
  22820. mess with tabs, most of the time!)  In my source, both lines begin
  22821. with two tabs.  So I suspect that you've been editing the file in an
  22822. editor that believes in 4 spaces per tab and somehow the editor
  22823. decided to replaces the tabs on the first line by the "equivalent"
  22824. number of spaces (e.g. because you touched the line with the cursor).
  22825.  
  22826. There are several ways to fix this:
  22827.  
  22828. (0) Find a clean backup of the library files with all the tabs in
  22829. them.  (This is probably necessary anyway, to avoid surprises.)
  22830.  
  22831. (1) Change your editor's idea of tab size (this may or may not be
  22832. possible).
  22833.  
  22834. (2) Replace all tabs by a consistent number of spaces, and never use
  22835. tabs when editing.  Now it doesn't matter how many spaces you use as
  22836. long as you are consistent (Python doesn't require indented lines to
  22837. be indented by a specific number of spaces, as long as you are
  22838. consistent.)  The function expandtabs() in string.py shows how to do
  22839. this in Python.
  22840.  
  22841. (3) Use an undocumented feature of the Python interpreter which
  22842. changes Python's idea of how many spaces a tab is worth: in the
  22843. beginning of each file, insert a comment of the form
  22844.  
  22845.     # vi:set tabsize=N:
  22846.  
  22847. where N is the desired tab size (e.g. 4).
  22848.  
  22849.  
  22850. Note that if you *only* use spaces for indentation you never run into
  22851. trouble; if you *only* use tabs, it likewise can't go wrong; but if
  22852. you start mixing the two, it's important that your editor and Python
  22853. have the same idea of tab size.  There should probably be a command
  22854. line option to set the interpreter's default tab size (in fact on the
  22855. Mac the default is 4 because all editors there use that default...).
  22856.  
  22857. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  22858. "You are all individuals!" "We are all individuals!"
  22859. 
  22860. 
  22861. Replied: Thu, 13 Aug 1992 11:36:22 +0200
  22862. Replied: "python-list "
  22863. Received: from cd4380.ufh.ac.za by charon.cwi.nl with SMTP
  22864.     id AA19141 (5.65b/2.10/CWI-Amsterdam); Thu, 13 Aug 1992 10:31:35 +0200
  22865. Received: from csgate.ufh.ac.za by cd4380.ufh.ac.za (5.61/1.34)
  22866.     id AA28342; Thu, 13 Aug 92 06:29:38 -0200
  22867. Received: From FS2/WORKQUEUE by csgate.ufh.ac.za
  22868.           via Charon 3.4 with IPX id 100.920813102735.320;
  22869.           13 Aug 92 10:26:56 -0200
  22870. Message-Id: <MAILQUEUE-101.920813102722.288@cs.ufh.ac.za>
  22871. To: python-list@cwi.nl
  22872. From: "Stoffel Erasmus"  <ERASMUS@cs.ufh.ac.za>
  22873. Date:         13 Aug 92 10:27:22 GMT+200
  22874. Subject:      Minor bug, perhaps? + (PC-Python/CS1)
  22875. X-Mailer:     Pegasus Mail v2.3 (R2).
  22876.  
  22877. Guido,
  22878.  
  22879. We decided that, being 3/5 thru the year, a bit of bi-linguality won't
  22880. do our CS1's any harm.  We have thus started moving from ABC to Python
  22881. (and back).  To me it is quite an exhilirating experience --- working
  22882. out `cheat sheets' to help them with translating, etc.  We'll expect
  22883. them to transliterate some of the code segments in the ABC Handbook as
  22884. well.  Python's `teachability' is being confirmed by this exercise, IMHO.
  22885. (CS1 is writing the first ABC vs Python test tomorrow morning.)
  22886.  
  22887. We are using PC-Python, and thus had to rediscover the usefulness of the
  22888. `raw' DOS command-line-editing function keys (F1..F6).  Otherwise I'm
  22889. using QuickEdit as a front end to the interpreter.
  22890.  
  22891. There is one small fly (bug?) in the ointment though!  When we try to
  22892. import the `string' module, the following happens:
  22893.  
  22894. Parsing error: file K:\Python\Lib\string.py: line 60:
  22895.                j = i
  22896.                ^
  22897. Syntax error: invalid syntax
  22898.  
  22899. Here we have an assignment statement following directly on a `one-line' if:
  22900.         ...
  22901.         if i==n: break
  22902.         j = i
  22903.         ...
  22904.  
  22905. What makes me suspicious is the fact that I encounter a similar problem
  22906. in the following code segment:
  22907.  
  22908.         from math import sqrt
  22909.  
  22910.         def smallest_divisor(n):
  22911.            if (n % 2 == 0): return 2
  22912.            try = 3
  22913.            while try <= sqrt(n):
  22914.               if n % try == 0: return try
  22915.               try = try+2
  22916.               ^
  22917.            return n
  22918.  
  22919.         print smallest_divisor(287)
  22920.  
  22921. Is there a work-around perhaps?
  22922.  
  22923. If anybody on the Python-list is interested in criticizing some of my
  22924. efforts, I would be quite happy to exhange ideas & handouts.
  22925.  
  22926. Regards,
  22927.  
  22928. Stoffel.
  22929.  
  22930. 
  22931. 
  22932. Replied: Fri, 14 Aug 1992 10:56:32 +0200
  22933. Replied: "brannon@jove.cs.caltech.edu (Terrence M. Brannon) "
  22934. Received: from jove.cs.caltech.edu by charon.cwi.nl with SMTP
  22935.     id AA00264 (5.65b/2.10/CWI-Amsterdam); Thu, 13 Aug 1992 19:12:19 +0200
  22936. Received: from stun4s.cs.caltech.edu by jove.cs.caltech.edu (4.1/1.34.1)
  22937.     id AA07358; Thu, 13 Aug 92 09:49:38 PDT
  22938. Date: Thu, 13 Aug 92 09:49:38 PDT
  22939. From: brannon@jove.cs.caltech.edu (Terrence M. Brannon)
  22940. Message-Id: <9208131649.AA07358@jove.cs.caltech.edu>
  22941. Received: by stun4s.cs.caltech.edu (4.1/SMI-4.1)
  22942.     id AA02428; Thu, 13 Aug 92 09:50:19 PDT
  22943. To: python-list@cwi.nl
  22944. Subject: Python FAQ
  22945. Comments: Hyperbole mail buttons accepted, v3.02P.
  22946.  
  22947. This version of the FAQ will be released to Usenet after I hear your
  22948. comments. I am out of town until Monday so I wont get back to you
  22949. until then.
  22950.  
  22951. * Menu:
  22952.  
  22953. * Introduction::        Introduction
  22954. * Trivia::            Trivia
  22955. * Overview of Available Modules::  Overview of Available Modules
  22956. * Common Questions::        
  22957. * Advanced Questions::        Advanced Questions
  22958. * Additional Information::    
  22959. * Obtaining::            Obtaining
  22960.  
  22961.  
  22962. Introduction
  22963. ************
  22964.  
  22965. Welcome to the Python Frequently Asked Questions List. This list is
  22966. maintained by a neophyte to Python and its purpose is to serve as a
  22967. reference source for questions that I have had as a beginner that
  22968. others will most likely have also.
  22969.  
  22970. In case you don't know what Python is, it is an object-oriented,
  22971. extensible, interpreted programming language that bridges the gap
  22972. between C and shell prorgramming and is thus ideally suited for
  22973. "throw-away" programming and rapid prototying but has also been used for
  22974. major software development.
  22975.  
  22976. It competes well with TCL, Perl and Emacs Lisp - three popular
  22977. feature-packed interpreted languages. Among its strong points:
  22978.  
  22979.    * The STDWIN class which allows the programmer to use generic graphics
  22980.      requests and events so that programs are immediate portable to
  22981.      X-Windows, Macintosh, Atari ST, and IBM PC.
  22982.    * The regular expression module allows for easy string manipulation.
  22983.    * Extensible through the development of new classes or new modules.
  22984.    * Python routines are calling from the C programming language as well as
  22985.      experimentally from Emacs Lisp.
  22986.  
  22987. The latest version of the Python FAQ is available by contacting the
  22988. author. Requests for the latest version or any other suggestions should
  22989. be sent to brannon@jove.cs.caltech.edu
  22990.  
  22991. Email jbw@cs.bu.edu for details about the League for Programming
  22992. Freedom.
  22993.  
  22994. Trivia
  22995. ******
  22996.  
  22997.  
  22998. The Author
  22999. ==========
  23000. The author of Python, Guido van Rossum, received a Master's degree in
  23001. mathematics and 
  23002. computer science from the University of Amsterdam in 1982.  He has
  23003. since been employed by CWI, a government-sponsored research institute
  23004. in the Netherlands employing about 200 people.  CWI's research themes
  23005. include large areas of computer science and mathematics, pure as well
  23006. as applied.  Guido has been involved in several projects at CWI: the
  23007. design and implementation of ABC, a programming language and
  23008. environment for programming by non-expert users, and the Amoeba
  23009. project, a highly advanced distributed operating system developed
  23010. jointly with the Free University of Amsterdam.  In 1991 he joined a
  23011. newly formed multimedia research project at CWI, which is
  23012. investigating issues of synchronization of independent data streams,
  23013. as well as operating support and user interfaces for multimedia
  23014. applications.  In his "spare time" he developed several freely
  23015. available software packages such as STDWIN, a library for portable use
  23016. of windowing interfaces and Python, an interpreted prototyping and
  23017. scripting language.  He can be reached electronically at guido@cwi.nl.
  23018.  
  23019. (If you want to summarize his current work and interests in one word,
  23020. "multimedia" would be it.)
  23021.  
  23022.  
  23023. Since he named the programming language Python, he obviously likes to
  23024. watch Monty Python programs.
  23025.  
  23026. He also has some sort of program that automatically generates curious
  23027. quotes. Some of the quirkiest:
  23028.  
  23029.      --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  23030.      "Kemal Ataturk had an entire menagerie called Abdul"
  23031.  
  23032.      --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  23033.      "Did you say knives?"
  23034.      "Rotating knives, yes"
  23035.  
  23036.      --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  23037.      "Repo Man's got all night, *every* night"
  23038.  
  23039. The Language
  23040. ============
  23041.  
  23042.    * The language is named after Monty Python, not a reptile.
  23043.    * There are two operators to test for inequality `<>' and `!='
  23044.      because Guido could not figure which he liked best
  23045.  
  23046. Overview of Available Modules
  23047. *****************************
  23048.  
  23049. For each type of module (built-in, standard, etc.) an exhaustive listing
  23050. of modules is not given. Instead, the more powerful and useful ones are
  23051. listed in order to whet your appetite for Python.
  23052.  
  23053.  
  23054. Built-In Modules
  23055. ================
  23056.  
  23057. regex
  23058. -----
  23059.  
  23060. The `regex' module allows you to set up configurable regular
  23061. expressions which by default uses Emacs-style matching. For more info
  23062. on denoting literal characters in Python see *Note Common Questions::.
  23063.  
  23064. marshal
  23065. -------
  23066.  
  23067. The `marshal' module allows you to write Python values in a
  23068. machine-independant format so that a program which writes data to a file
  23069. will port across architectures
  23070.  
  23071. Standard Modules
  23072. ================
  23073.  
  23074. string
  23075. ------
  23076.  
  23077. Although there are a host of string operations available, the most
  23078. salient ones are listed below:
  23079.  
  23080.    * String splitting based on a string. No string splitting based on a
  23081.      regexp. Given the string `'Bill Jones:2424 Klickitat St.:Iceland'' we
  23082.      can create a list of the colon-separated fields as follows:
  23083.           string.split('Bill Jones:2424 Klickitat St.:'Iceland' , ':')
  23084.      with the result being
  23085.           ['Bill Jones', '2424 Klickitat St.', 'Iceland']
  23086.    * String creation by placing a string separator between the string items of a
  23087.      tuple or list. For example, to create the colon-separated string above
  23088.      from the list above we would issue the following command:
  23089.           string.join(['Bill Jones', '2424 Klickitat St.', 'Iceland'], ':') 
  23090.    * Removal of whitespace. Given the string `'   a b    '' the Python
  23091.      command 
  23092.           string.strip('     a b     ')
  23093.      would result in the string `'a b''
  23094.    * Substring search. Given the string `'ahelluvashow'' the Python
  23095.      command
  23096.           string.index('ahelluvashow' , 'luva')
  23097.      would return the integer 4
  23098.    * Right, left, and center justification of strings. For example, we can
  23099.      take the following strings: 'horse', 'mule' , 'ox' , 'guido' and by
  23100.      repeatedly applying string.rjust(s, 10) where s is each of the above
  23101.      strings we can get the following output:
  23102.                horse
  23103.                 mule
  23104.                   ox
  23105.                guido        
  23106.  
  23107. stdwin
  23108. ------
  23109.  
  23110. Using the stdwin module, the Python (and C) programmer can create
  23111. source code with graphic windowing which will run on all of the
  23112. following platforms:
  23113.  
  23114.    * X-Windows
  23115.    * Macintosh using Think C 4.0
  23116.    * Alphanumeric Terminals
  23117.    * MS-DOS with a dumb display using Microsoft C
  23118.    * Atari ST using MWC or Turbo C
  23119.  
  23120. Of course no window system's potential is maximized under stdwin, but
  23121. this is offset by the portability advantage.
  23122.  
  23123. A brief vignette of methods in the stdwin module:
  23124.  
  23125.    * event polling
  23126.    * scrollbars
  23127.    * color fetching
  23128.    * semi-automatic menu widget creation
  23129.    * cut buffers
  23130.    * point, line, box drawing routines
  23131.    * text widgets
  23132.  
  23133. unix
  23134. ----
  23135.  
  23136. There is a BSD 4.3 socket interface. There is a posix module that
  23137. provides access to to operating system functionality that is
  23138. standardised by the C Standard and the POSIX standard. For example, one
  23139. automatically gets a dictionary of environmental variables. It allows
  23140. for changing the current working directory, forking, directory listing,
  23141. obtaining the current and parent process id and so forth. Another module
  23142. named `posixpath' allows one to easily perform file
  23143. naming and testing (ie, basename of a directory path, intelligently
  23144. join two directory paths, filename or directory tests).
  23145.  
  23146. Common Questions
  23147. ****************
  23148.  
  23149.    * How do you print something without having a carriage return appended to
  23150.      it? 
  23151.  
  23152.      You end your print statement with a comma. For example,
  23153.           i = 5
  23154.           print 'i = ',
  23155.           print i
  23156.      will result in:
  23157.      `i = 5'
  23158.      as output.
  23159.    * Do I have to leave the interpreter each time I want a modules source
  23160.      code re-evaluated?
  23161.  
  23162.      No. If your module name is shc.py and you have loaded it into the
  23163.      `Python' interpreter. Type the following exactly: `reload(shc)'
  23164.  
  23165.    * What are these numbers that show up after I do certain statements?
  23166.  
  23167.      The Python interpreter prints the value of an expression statement
  23168.      unless it is none -- this is done so you can use the interpreter as an
  23169.      interactive calculator. The simplest way of avoiding the printing of the
  23170.      value is to assign it to a dummy value.
  23171.  
  23172.    * If I am looking for the string "\section{Loving Make}" why doesnt the
  23173.      following regexp work: `sect_regex = '\\section{\(.*\)} .*''
  23174.  
  23175.      To include a literal backslash in a regexp you must double it, but to
  23176.      include one in a Python string literal you must QUADRUPLE it. So the
  23177.      correct regexp is: `sect_regex = '\\\\section{.*}.*''
  23178.  
  23179.      But the `\(' and `\)' worked fine!
  23180.  
  23181.      As stated in the reference manual under "String Literals," a backslash
  23182.      that is followed by a character that is not one of the Standard C escape
  23183.      sequences such as `\n' or `\t' stays in the string. As far as
  23184.      I know the only character that has a special meaning following a
  23185.      backslash in regular expressions as well as a Python string literal is
  23186.      the backslash.
  23187.  
  23188.    * Ok, I am in the interpreter and I want to run a Python script and pass
  23189.      it command line arguments. Can I do it?
  23190.  
  23191.      Sure. Two methods:
  23192.  
  23193.        1.      import sys
  23194.                sys.argv = [ '-c' , 'red' ]
  23195.                execfile('colormap')
  23196.        2.      import os
  23197.                os.system('colormap -c red')
  23198.  
  23199.  
  23200. Advanced Questions
  23201. ******************
  23202.  
  23203.  
  23204.   1. From gerry@camscan.co.uk:
  23205.      How can I best to put a Python interpreter into, say, 12
  23206.      processes given that each interpreter needed to interface to process
  23207.      specific not just general library code and I don't
  23208.      want the overhead of the core Python code in every process
  23209.      given that I'd mananged to produce a shared library version of it on
  23210.      my SUN
  23211.  
  23212.      Answer:
  23213.      It sounds like your problem is that you want to use a shared library
  23214.      for most of the code of the Python interpreter, but you want each
  23215.      interpreter to have different extensions, and you don't want to have
  23216.      to build 12 different shared libraries (which would undo most of the
  23217.      benefit of shared libraries).
  23218.  
  23219.      Assuming you aren't using threads (all processes sharing the same
  23220.      address space for data), it should be possible to do this using only a
  23221.      tiny change in the code: move the initialized array of module init
  23222.      functions (inittab in "config.c") out of the shared library into the
  23223.      main programs, and initialize it differently in each program.  Or
  23224.      allocate some extra slots in it and dynamically add extra entries
  23225.      before initializing the interpreter.  The table is searched each time
  23226.      a module isn't found, so you may even add entries even dynamically --
  23227.      Python postpones the search for and the call to a module's init
  23228.      function until the first time it is imported.
  23229.  
  23230.      Alternatively, you could use dynamic loading (read misc/DYNLOAD in the
  23231.      distribution), but this would probably be slow and seems unnecessary.
  23232.  
  23233.           --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  23234.           "That was never five minutes just now!"
  23235.           "I'm afraid it was."
  23236.  
  23237. Additional Information
  23238. **********************
  23239.  
  23240. There are two python modes for emacs:
  23241.  
  23242.    * A standard one packed with the Python distribution (in
  23243.      $DISTRIB/misc/python-mode.el where DISTRIB==the top level of where
  23244.      Python was untarred to) that works with the
  23245.      shell mode that is still common in Emacs.
  23246.    * An experimental mode by Terrence Brannon that works with comint (which along with
  23247.      cmushell will replace the current shell-mode in Emacs 19) and allows one
  23248.      to have a startup file named `.python' in which you can
  23249.      automatically load in often-used modules. It also robustly facilitates
  23250.      interprocess communication between Emacs and Python -- simple types as
  23251.      well as lists may be passed freely between Emacs and Python. Other types
  23252.      such as Python's dictionaries and Emacs' vectors and association and
  23253.      property lists do not have converter routines yet written.
  23254.  
  23255.  
  23256. Obtaining
  23257. *********
  23258.  
  23259. Anonymous FTP to ftp.cwi.nl (IP address 192.16.184.180) in /pub filename
  23260. `python*.tar.Z' where * stands for a version number. This is a
  23261. compressed UNIX tar file containing the C source and LaTeX documentation
  23262. for the Python interpreter. It includes the Python library modules and
  23263. the `Stubcode' interpreter as well as many example Python programs.
  23264. Total disk space occupied by the distribution is about 3 megabytes;
  23265. compilation requires 1-3 megabytes depending on the configuration built,
  23266. the compile options, and so forth.    
  23267.  
  23268.  
  23269. Terrence Brannon       (brannon@jove.cs.caltech.edu)  
  23270. medical biology via acupuncture and particle physics
  23271.  
  23272.  
  23273.  
  23274. 
  23275. 
  23276. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  23277.     id AA02292 (5.65b/2.10/CWI-Amsterdam); Thu, 13 Aug 1992 21:04:10 +0200
  23278. Received: from server.cs.virginia.edu by uvaarpa.Virginia.EDU id aa00458;
  23279.           13 Aug 92 15:00 EDT
  23280. Received: from banyan.cs.Virginia.EDU by uvacs.cs.Virginia.EDU (4.1/5.1.UVA)
  23281.     id AA11195; Thu, 13 Aug 92 15:00:17 EDT
  23282. Posted-Date: Thu, 13 Aug 92 15:00:12 EDT
  23283. Return-Path: <spm2d@banyan.cs.Virginia.EDU>
  23284. Received: by banyan.cs.Virginia.EDU (4.1/SMI-2.0)
  23285.     id AA17410; Thu, 13 Aug 92 15:00:12 EDT
  23286. Date: Thu, 13 Aug 92 15:00:12 EDT
  23287. From: spm2d@banyan.cs.Virginia.EDU
  23288. Message-Id: <9208131900.AA17410@banyan.cs.Virginia.EDU>
  23289. To: python-list@cwi.nl
  23290. Subject: Question about Macintosh python
  23291.  
  23292. Has anyone bothered to implement sockets for the Mac version of
  23293. Python?
  23294.  
  23295. Thanks,
  23296.  
  23297. Steve
  23298. 
  23299. 
  23300. Replied: Sun, 16 Aug 1992 20:31:21 +0200
  23301. Replied: louk@RESEARCH.TELERIDE.ON.CA
  23302. Received: from uunet.ca by charon.cwi.nl with SMTP
  23303.     id AA22864 (5.65b/2.10/CWI-Amsterdam); Sun, 16 Aug 1992 17:06:04 +0200
  23304. Received: from tslwat by mail.uunet.ca with UUCP id <9781>; Sun, 16 Aug 1992 11:05:57 -0400
  23305. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23306.     id <m0mJmF6-0002AQC@research.teleride.on.ca>; Sun, 16 Aug 92 11:10 EDT
  23307. Message-Id: <m0mJmF6-0002AQC@research.teleride.on.ca>
  23308. Date:     Sun, 16 Aug 1992 11:10:00 -0400
  23309. From: louk@RESEARCH.TELERIDE.ON.CA (Lou Kates)
  23310. To: python-list@cwi.nl
  23311. Subject: splitfields
  23312.  
  23313.  
  23314. string.splitfields('abc','') appears to go into an endless loop.  I would
  23315. have thought that it would return ['a','b','c'].
  23316.  
  23317. Lou Kates, louk@research.teleride.on.ca
  23318. 
  23319. 
  23320. Replied: Mon, 17 Aug 1992 10:35:09 +0200
  23321. Replied: "louk@research.teleride.on.ca (Lou Kates) "
  23322. Received: from uunet.ca by charon.cwi.nl with SMTP
  23323.     id AA08100 (5.65b/2.10/CWI-Amsterdam); Mon, 17 Aug 1992 02:49:09 +0200
  23324. Received: from tslwat by mail.uunet.ca with UUCP id <10155>; Sun, 16 Aug 1992 20:48:48 -0400
  23325. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23326.     id <m0mJvDN-00016qC@research.teleride.on.ca>; Sun, 16 Aug 92 20:44 EDT
  23327. Message-Id: <m0mJvDN-00016qC@research.teleride.on.ca>
  23328. From: louk@research.teleride.on.ca (Lou Kates)
  23329. Subject: Re: splitfields (fwd)
  23330. To: python-list@cwi.nl
  23331. Date:     Sun, 16 Aug 1992 20:44:56 -0400
  23332. X-Mailer: ELM [version 2.3 PL11]
  23333.  
  23334. > >string.splitfields('abc','') appears to go into an endless loop.  I would
  23335. > >have thought that it would return ['a','b','c'].
  23336. > What makes you think so?  I believe a reasonable mathematical
  23337. > definition of splitfields(x, y) would return an infinite list if y==''.
  23338.  
  23339. The following seem to be reasonable properties for splitfields to
  23340. have for all strings s and t:
  23341.  
  23342.     1. joinfields(splitfields(s, t), t) == s
  23343.     2. splitfields(joinfields(s, t), t) == s
  23344.     3. splitfields(s, t) finite
  23345.  
  23346. That is,  for identical separators,  t, splitfields commutes with
  23347. joinfields and is finite for finite s. In other words, joinfields
  23348. and splitfields  are  inverses of  each other and splitfields  is
  23349. finite for finite s.
  23350.  
  23351. Note that
  23352.  
  23353.     joinfields(['abc'],'')
  23354.     joinfields(['a','b','c'],'')
  23355.     joinfields(['ab','c'],'')
  23356.     joinfields(['a','bc'],'')
  23357.     joinfields(['','abc'],'')
  23358.     etc.
  23359.  
  23360. are all the same so that splitfields  is not 1-to-1 and so has no
  23361. inverse (which is what properties #1 and #2 imply).
  23362.  
  23363. I suppose  that going  into an   endless loop could represent the
  23364. fact  that   there  is no  unique  inverse to  any of the  above;
  23365. however, I think it  would  be more  useful to pick  one of  them
  23366. (this might correspond to picking the  generalized  inverse  of a
  23367. matrix which  is  a   similar  situation)   or else  generate  an
  23368. exception.
  23369.  
  23370. If we were to pick one of them 
  23371.  
  23372.     splitfields('abc','') == ['a','b','c']
  23373.  
  23374. seems generally the most useful since we already have an easy way
  23375. of converting  'abc' to ['abc'] simply  by enclosing it in square
  23376. brackets.
  23377.  
  23378. -- 
  23379. Lou Kates, louk@research.teleride.on.ca
  23380. 
  23381. 
  23382. Received: from uunet.ca by charon.cwi.nl with SMTP
  23383.     id AA11657 (5.65b/2.10/CWI-Amsterdam); Wed, 19 Aug 1992 20:19:03 +0200
  23384. Received: from tslwat by mail.uunet.ca with UUCP id <10624>; Wed, 19 Aug 1992 14:17:40 -0400
  23385. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23386.     id <m0mKtwW-0001SXC@research.teleride.on.ca>; Wed, 19 Aug 92 13:35 EDT
  23387. Message-Id: <m0mKtwW-0001SXC@research.teleride.on.ca>
  23388. From: louk@research.teleride.on.ca (Lou Kates)
  23389. Subject: Re: more on splitfields 
  23390. To: python-list@cwi.nl
  23391. Date:     Wed, 19 Aug 1992 13:35:34 -0400
  23392. X-Mailer: ELM [version 2.3 PL11]
  23393.  
  23394. > >
  23395. > >    a = string.splitfields(s, sep)
  23396. > >
  23397. > I currently think that the best thing to do is to raise an exception
  23398. > when sep is ''.  First, any one of the possible results are easy to
  23399. > get with other means.  Since almost all calls will specify a literal
  23400. > as separator argument, I doubt that people will want to use
  23401. > splitfields() for what they can express easier.  Second, there is an
  23402.  
  23403. Actually the way this entire thing  came about what that I wanted
  23404. to derive a list of single characters from a string. Of course, I
  23405. could write a simple loop but thought that perhaps there might be
  23406. an easier  way  using some library function/method. Is there such
  23407. an easier way?
  23408.  
  23409. > anomaly for splitfields('',''): in to your previous proposal it should
  23410. > return [], but joinfields([], sep ) is ill-defined: the list must have
  23411. > at least one element!  So an exception seems to be the best solution.
  23412.  
  23413. OK. I agree with you.
  23414.  
  23415. While  we    are  on  the  topic   of infinite  loops in  library
  23416. functions/methods the following will also cause infinite looping:
  23417.  
  23418.     a = [2, 'abc']
  23419.     a.append(a)
  23420.  
  23421. There are a number of possibilities:
  23422.  
  23423.     1.   let it loop infinitely as it does now
  23424.  
  23425.     2.   allow circularity
  23426.  
  23427.     3.   regard this to be the same as:
  23428.  
  23429.         a.append(deepcopy(a))
  23430.  
  23431.          in which   case  there is   no circularity. In  this
  23432.          example we would get:
  23433.          
  23434.         [2, 'abc', [2, 'abc']]
  23435.  
  23436.     4.   detect circularity and raise an exception
  23437.  
  23438. I suppose  that there are  significant implementation issues here
  23439. as well as desirability ones.
  23440.  
  23441. Lou Kates, louk@research.teleride.on.ca
  23442.  
  23443. 
  23444. 
  23445. Received: from hopscotch.ksr.com by charon.cwi.nl with SMTP
  23446.     id AA01317 (5.65b/2.10/CWI-Amsterdam); Wed, 19 Aug 1992 23:31:14 +0200
  23447. Received: from ksr.com (frankenstein.ksr.com) by hopscotch.ksr.com with SMTP
  23448.     id AA23347; Wed, 19 Aug 1992 17:29:45 -0400
  23449. Received: from kaos.ksr.com by ksr.com (4.0/SMI-3.2)
  23450.     id AA12323; Wed, 19 Aug 92 17:30:39 EDT
  23451. Received: by kaos.ksr.com (4.0/KSR-2.0)
  23452.     id AA08898; Wed, 19 Aug 92 17:30:38 EDT
  23453. Message-Id: <9208192130.AA08898@kaos.ksr.com>
  23454. To: louk@research.teleride.on.ca (Lou Kates)
  23455. Subject: Self-referential objects (was Re: more on splitfields)
  23456. Cc: python-list@cwi.nl
  23457. Date: Wed, 19 Aug 92 17:30:36 -0400
  23458. From: Tim Peters <tim@ksr.com>
  23459.  
  23460. >  ...
  23461. >  While  we    are  on  the  topic   of infinite  loops in  library
  23462. >  functions/methods the following will also cause infinite looping:
  23463. >
  23464. >      a = [2, 'abc']
  23465. >      a.append(a)
  23466. >
  23467. >  There are a number of possibilities:
  23468. >  ...
  23469.  
  23470. Works fine for me, Lou:
  23471.  
  23472. Python 0.9.6 (>= 1 Jan 1992).
  23473. Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  23474. >>> a = [2,'abc']
  23475. >>> a.append(a)
  23476. >>> len(a)
  23477. 3
  23478. >>> (a is a[2])
  23479. 1
  23480. >>>
  23481.  
  23482. This is a UNIX(tm) flavor of Python.  Suspect you're looking at an
  23483. installation problem, or are trying to print the thing (& the docs warn
  23484. against trying to print self-referential objects -- you do get into an
  23485. infinite loop then).
  23486.  
  23487. personally-already-like-what-python-does-with-this-ly y'rs  - tim
  23488.  
  23489. Tim Peters   Kendall Square Research Corp
  23490. tim@ksr.com,         ksr!tim@uunet.uu.net
  23491. 
  23492. 
  23493. Replied: Thu, 20 Aug 1992 10:19:05 +0200
  23494. Replied: "louk@research.teleride.on.ca (Lou Kates) "
  23495. Received: from uunet.ca by charon.cwi.nl with SMTP
  23496.     id AA17053 (5.65b/2.10/CWI-Amsterdam); Thu, 20 Aug 1992 06:03:30 +0200
  23497. Received: from tslwat by mail.uunet.ca with UUCP id <10743>; Thu, 20 Aug 1992 00:03:19 -0400
  23498. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23499.     id <m0mL3na-0001SVC@research.teleride.on.ca>; Thu, 20 Aug 92 00:07 EDT
  23500. Message-Id: <m0mL3na-0001SVC@research.teleride.on.ca>
  23501. From: louk@research.teleride.on.ca (Lou Kates)
  23502. Subject: Re: Self-referential objects (was Re: more on splitfields)
  23503. To: tim@ksr.com (Tim Peters)
  23504. Date:     Thu, 20 Aug 1992 00:07:01 -0400
  23505. Cc: python-list@cwi.nl
  23506. In-Reply-To: <9208192130.AA08898@kaos.ksr.com>; from "Tim Peters" at Aug 19, 92 5:30 pm
  23507. X-Mailer: ELM [version 2.3 PL11]
  23508.  
  23509. > >  ...
  23510. > >  While  we    are  on  the  topic   of infinite  loops in  library
  23511. > >  functions/methods the following will also cause infinite looping:
  23512. > >
  23513. > >      a = [2, 'abc']
  23514. > >      a.append(a)
  23515. > >
  23516. > Works fine for me, Lou:
  23517. > Python 0.9.6 (>= 1 Jan 1992).
  23518. > Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam
  23519. > >>> a = [2,'abc']
  23520. > >>> a.append(a)
  23521.  
  23522. The version of Python that I have is 0.9.1.  Evidently there is a difference
  23523. between the two versions.
  23524.  
  23525. Lou Kates, louk@research.teleride.on.ca
  23526. 
  23527. 
  23528. Replied: Mon, 31 Aug 1992 10:56:35 +0200
  23529. Replied: "jcv26@cas.org (Jon Vander Hill) python-list@cwi.nl"
  23530. Received: from meermin.cwi.nl by charon.cwi.nl with SMTP
  23531.     id AA19418 (5.65b/2.10/CWI-Amsterdam); Fri, 28 Aug 1992 21:40:03 +0200
  23532. Received: from srv01s4.cas.org by meermin.cwi.nl with SMTP
  23533.     id AA14012 (5.65a/2.10/CWI-Amsterdam); Fri, 28 Aug 1992 19:40:00 GMT
  23534. Date: Fri, 28 Aug 92 15:37:32 EDT
  23535. From: jcv26@cas.org (Jon Vander Hill)
  23536. Message-Id: <9208281937.AA07600@cas.org>
  23537. To: python-list@cwi.nl
  23538. Subject: miniedit.py
  23539.  
  23540.  
  23541. I'm trying to get miniedit.py to run.
  23542. My system particulars are as follows:
  23543.  
  23544. SunOS 4.1.2
  23545. OpenWindows 3.0
  23546. Python 0.9.7beta
  23547. Stdwin 0.9.7 (X11 port)
  23548.  
  23549. I'm trying to run the application directly from a shell (via the #!
  23550. mechanism).  The application puts up its window, but doesn't load the
  23551. file given on the command line and does not seem to respond to input
  23552. events.  I'm able to kill it by generating a SIGINT in the shell I
  23553. start it from and move the mouse cursor into the application window.
  23554.  
  23555. If it helps, the microedit.py application comes up, loads the file,
  23556. and responds to input events, so I'm thinking the problem
  23557. is in the miniedit application itself or possibly the python stdwin library.
  23558.  
  23559. I'm pretty new to python, so keep any advice in the simplest terms.
  23560.  
  23561. Jon Vander Hill
  23562. jon@cas.org
  23563. 
  23564. 
  23565. Forwarded: Fri, 04 Sep 1992 14:09:59 +0200
  23566. Forwarded: "jack "
  23567. Forwarded: Wed, 02 Sep 1992 10:17:11 +0200
  23568. Forwarded: "jack "
  23569. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  23570.     id AA17845 (5.65b/2.10/CWI-Amsterdam); Mon, 31 Aug 1992 10:56:37 +0200
  23571. Received: by voorn.cwi.nl with SMTP; Mon, 31 Aug 1992 08:56:36 GMT
  23572. Message-Id: <9208310856.AA08439@voorn.cwi.nl>
  23573. To: jcv26@cas.org (Jon Vander Hill)
  23574. Cc: python-list@cwi.nl
  23575. Subject: Re: miniedit.py 
  23576. In-Reply-To: Your message of "Fri, 28 Aug 1992 15:37:32 MDT."
  23577.              <9208281937.AA07600@cas.org> 
  23578. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  23579. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  23580. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  23581. Date: Mon, 31 Aug 1992 10:56:35 +0200
  23582. Sender: Guido.van.Rossum@cwi.nl
  23583.  
  23584. >I'm trying to get miniedit.py to run.
  23585. >My system particulars are as follows:
  23586. >
  23587. >SunOS 4.1.2
  23588. >OpenWindows 3.0
  23589. >Python 0.9.7beta
  23590. >Stdwin 0.9.7 (X11 port)
  23591. >
  23592. >I'm trying to run the application directly from a shell (via the #!
  23593. >mechanism).  The application puts up its window, but doesn't load the
  23594. >file given on the command line and does not seem to respond to input
  23595. >events.  I'm able to kill it by generating a SIGINT in the shell I
  23596. >start it from and move the mouse cursor into the application window.
  23597.  
  23598. I'm sorry: there's a bug in 0.9.7beta version (that's why it's a
  23599. beta version!), which make it think that certain objects are "false"
  23600. when they should actually be considered "true".
  23601.  
  23602. There have been several releases labeled "0.9.7beta", and I'm not sure
  23603. the current version still has the bug in it -- there's another bug I
  23604. need to hunt down before I dare to make a new beta release.  Anyway,
  23605. here's a description of a fix.  Edit the file "ceval.c" and find the
  23606. definition of function testbool().  There is a sequence of code ending
  23607. like this:
  23608.  
  23609.     else if (v->ob_type->tp_as_sequence != NULL)
  23610.         res = (*v->ob_type->tp_as_sequence->sq_length)(v);
  23611.     else
  23612.         res = 0;
  23613.  
  23614. The 0 in the last line should be a 1.  That's all there is to the fix!
  23615.  
  23616. Happy hacking...
  23617.  
  23618. (BTW, I'm extremely glad that you listed your environment so carefully
  23619. -- without it I would have had a hard time this was for real.)
  23620.  
  23621. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  23622. "I'm sorry, is this a five-minute argument, or the full half-hour?"
  23623. 
  23624. 
  23625. Replied: Wed, 02 Sep 1992 21:43:25 +0200
  23626. Replied: jcv26@cas.org (Jon Vander Hill)
  23627. Replied: python-list@cwi.nl
  23628. Received: from srv01s4.cas.org by charon.cwi.nl with SMTP
  23629.     id AA16927 (5.65b/2.10/CWI-Amsterdam); Wed, 2 Sep 1992 20:07:26 +0200
  23630. Date: Wed, 2 Sep 92 14:07:09 EDT
  23631. From: jcv26@cas.org (Jon Vander Hill)
  23632. Message-Id: <9209021807.AA19440@cas.org>
  23633. To: python-list@cwi.nl
  23634. Subject: difficulty with microedit.py on a Mac
  23635. Cc: jcv26@cas.org
  23636.  
  23637. Environment:
  23638.  
  23639. Mac Plus, 4Mb
  23640. System 6.0.5
  23641. Finder 6.1.5
  23642. Python 0.9.7 (from ftp.cwi.nl)
  23643.  
  23644. I placed microedit.py in the same folder as python.
  23645. I needed stdwinevents.py in order to import microedit.py.
  23646.  
  23647. I brought up python and ran the following commands:
  23648.  
  23649. import sys
  23650. sys.argv = ['','microedit.py']
  23651. import microedit
  23652.  
  23653. A window was created entitles "microedit.py".
  23654. The following message appeared on the title bar:
  23655.    zrealloc(0x2ec2a8,0): out of mem
  23656. I hit the return key.
  23657. The message "press <<return>> to exit" appeared on
  23658. the title bar of the python console.
  23659.  
  23660. Any help would be appreciated.
  23661.  
  23662. Also, is there any doc that describes what modules
  23663. are compiled into Python 0.9.7 for the Mac?
  23664.  
  23665. Jon Vander Hill, System Engineer              Chemical Abstracts Service
  23666. internet: jon@cas.org (preferred)             P.O. Box 3012 (preferred)
  23667.           jon@cas.bitnet                      2540 Olentangy River Road
  23668. uucp:     uunet!osu-cis!chemabs!jon           Columbus, Ohio 43210
  23669. bitnet:   jon@CAS                             USA
  23670. phone:    (614) 447-3600
  23671.  
  23672. 
  23673. 
  23674. To: jcv26@cas.org (Jon Vander Hill)
  23675. cc: python-list@cwi.nl
  23676. Subject: Re: difficulty with microedit.py on a Mac 
  23677. In-reply-to: Your message of "Wed, 02 Sep 1992 14:07:09 MDT."
  23678.              <9209021807.AA19440@cas.org> 
  23679. From: Guido van Rossum <guido@cwi.nl>
  23680. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  23681. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  23682. Date: Wed, 02 Sep 1992 21:43:24 +0200
  23683. Sender: guido
  23684.  
  23685. >Python 0.9.7 (from ftp.cwi.nl)
  23686. >The following message appeared on the title bar:
  23687. >   zrealloc(0x2ec2a8,0): out of mem
  23688.  
  23689. I'm sorry, the 0.9.7beta Mac Python binary for a while contained the
  23690. same bug as the sources there, as explained in a previous message to
  23691. this list.  I have placed a fixed Mac Python there around 30 August,
  23692. so if you fetched it before that date you should fetch a fresh copy
  23693. and try again.  (Note that the new file isn't compacted with stuffit.
  23694. I could fix it but I only occasionally have access to a Mac where I
  23695. can compile Python these days...)
  23696.  
  23697. >Also, is there any doc that describes what modules
  23698. >are compiled into Python 0.9.7 for the Mac?
  23699.  
  23700. Not really -- there probably should be...  Here's an excerpt from the
  23701. config file I used:
  23702.  
  23703. #define STDWIN 1
  23704. [...]
  23705.     /* Standard modules */
  23706.     {"time",    inittime},
  23707.     {"math",    initmath},
  23708.     {"regex",    initregex},
  23709.     
  23710.     /* Mac-specific modules */
  23711.     {"mac",        initmac},
  23712. #ifdef USE_STDWIN
  23713.     {"stdwin",    initstdwin},
  23714. #endif
  23715.     {"marshal",    initmarshal},
  23716.  
  23717. Note that the modules 'sys' and 'builtin' as well as '__main__' are
  23718. always present (see the Library reference manual for a description of
  23719. these).
  23720.  
  23721. BTW, for documented modules it is of course trivial to test whether
  23722. they exist or not:
  23723.  
  23724.     try:
  23725.         import foo
  23726.         print 'foo exists'
  23727.     except ImportError:
  23728.         print 'foo does not exist'
  23729.  
  23730. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  23731. "The lawnmower.  Surely such a gadget could not have been generated
  23732. independently in two separate areas."
  23733. 
  23734. 
  23735. Received: from newton.ncsa.uiuc.edu by charon.cwi.nl with SMTP
  23736.     id AA18565 (5.65b/2.10/CWI-Amsterdam); Wed, 2 Sep 1992 22:04:05 +0200
  23737. Received: from void.ncsa.uiuc.edu by newton.ncsa.uiuc.edu with SMTP id AA20905
  23738.   (5.65a/IDA-1.4.2 for Guido.van.Rossum@cwi.nl); Wed, 2 Sep 92 15:04:02 -0500
  23739. Return-Path: <liberte@ncsa.uiuc.edu>
  23740. Received: by void.ncsa.uiuc.edu (4.1/NCSA-4.1)
  23741.     id AA24485; Wed, 2 Sep 92 15:02:40 CDT
  23742. Date: Wed, 2 Sep 92 15:02:40 CDT
  23743. From: liberte@ncsa.uiuc.edu (Daniel LaLiberte)
  23744. Message-Id: <9209022002.AA24485@void.ncsa.uiuc.edu>
  23745. To: Guido.van.Rossum@cwi.nl
  23746. Subject: Re: difficulty with microedit.py on a Mac
  23747.  
  23748. If anyone wants to do GL like things on the mac, my previous work
  23749. on python should still apply, though it needs to be updated to
  23750. use VOGL instead of our NGL which we have dropped.  Just forward
  23751. requests to me.
  23752.  
  23753. dan
  23754. 
  23755. 
  23756. Replied: Tue, 08 Sep 1992 10:34:15 +0200
  23757. Replied: "louk@RESEARCH.TELERIDE.ON.CA (Lou Kates) "
  23758. Received: from uunet.ca by charon.cwi.nl with SMTP
  23759.     id AA06609 (5.65b/2.10/CWI-Amsterdam); Tue, 8 Sep 1992 06:04:05 +0200
  23760. Received: from tsltor by mail.uunet.ca with UUCP id <10356>; Mon, 7 Sep 1992 23:42:06 -0400
  23761. Received: from tslwat by falcon.teleride.on.ca with uucp
  23762.     (Smail3.1.26.7 #5) id m0mRw7U-0000dpC; Mon, 7 Sep 92 23:20 EDT
  23763. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23764.     id <m0mRvqn-0002j5C@research.teleride.on.ca>; Mon, 7 Sep 92 23:02 EDT
  23765. Message-Id: <m0mRvqn-0002j5C@research.teleride.on.ca>
  23766. From: louk@RESEARCH.TELERIDE.ON.CA (Lou Kates)
  23767. Subject: Python vs. awk
  23768. To: python-list@cwi.nl
  23769. Date:     Mon, 7 Sep 1992 23:02:44 -0400
  23770. X-Mailer: ELM [version 2.3 PL11]
  23771.  
  23772.  
  23773.  
  23774. The following  compares a  Python program   to one written in awk
  23775. (actually nawk).  I realize that Guido feels  that Python  should
  23776. not really be compared to awk or perl since its aims are somewhat
  23777. different but I still feel that this sort of exercise is useful.
  23778.  
  23779. The two programs below exchange two fields preserving whitespace.
  23780. These fields  can  be optionally   specified on the command  line
  23781. using the   easiest  way that  the two  languages support (getopt
  23782. library for Python and the x=12 form for awk). The Python program
  23783. is 27  lines compared  to  12 statements and 10 lines for the awk
  23784. program.
  23785.  
  23786. The following is the breakdown:
  23787.  
  23788.                 Python        Awk
  23789. Declaration statements        1 stmt        0 stmts
  23790. Option processing        5 stmts        1 stmts
  23791. Argument processing        2 stmts        0 stmts
  23792. Reading & looping over input    3 stmts        0 stmts
  23793. Field splitting            1 stmt        0 stmts
  23794. Get list of whitespace fields    9 stmts     3 stmts
  23795. Check number of fields        1 stmt        1 stmt
  23796. Loop to swap & print fields    5 stmts        6 stmts (4 lines)
  23797. End of block stmts        0 stmts        1 stmt
  23798.  
  23799. Total                27 stmt        12 stmts (10 lines)
  23800.  
  23801.  
  23802. The areas of saving for awk are:
  23803.  
  23804. 1.   Awk saves 9 lines by implicitly processing the command line,
  23805.      including options and arguments and reading and looping over
  23806.      the input as well as doing field splitting.
  23807.  
  23808. 2.   Awk's gsub   allows  one to   compactly obtain  the list  of
  23809.      whitespace. In Python you have to do it by hand.
  23810.  
  23811. 3.   With Python you have to build up  the output in a buffer and
  23812.      in awk you can use printf to avoid this.
  23813.  
  23814. My conclusions are that Python needs some additions to the library
  23815. to:
  23816.  
  23817. 1.   facilitate command processing, option processing and looping
  23818.      over input. An expanded version of the implicit loop program
  23819.      that was posted a while ago to this list could help here.
  23820.  
  23821. 2.   perform sub  and  gsub functions.   These could return a new
  23822.      string with   the replacement  done  since Python's  strings
  23823.      cannot be changed in place (immutable).
  23824.  
  23825. 3.   provide a way of printing out  a partial line without having
  23826.      a space placed  at the end of it to reduce the need to build
  23827.      up print buffers in the program.
  23828.  
  23829.  
  23830. === awk program ===
  23831.  
  23832.  
  23833. BEGIN { if (x == 0) x = 1; if (y == 0) y = 2 }
  23834. NF >= x && NF >=y {
  23835.     line = $0;
  23836.     gsub(/[^ \t]+/, ":", line);
  23837.     split(line, s, ":");
  23838.     t = $y; $y = $x; $x = t;
  23839.     for(i=1; i<=NF; i++) 
  23840.         printf("%s%s", s[i], $i);
  23841.     print "";
  23842. }
  23843.  
  23844.  
  23845. === Python program ===
  23846.  
  23847. import getopt, regexp, string, sys
  23848. pat = regexp.compile('[^ \t]+')
  23849. options, files = getopt.getopt(sys.argv[1:], 'x:y:')
  23850. x, y = 1, 2
  23851. for t in options:
  23852.     if 'x' in t[0]: x = string.atoi(t[1])
  23853.     if 'y' in t[0]: y = string.atoi(t[1])
  23854. fp = sys.stdin
  23855. if files: fp = sys.open(files[0])
  23856. while 1:
  23857.     line = fp.readline()
  23858.     if not line: break
  23859.     fl = string.split(line)
  23860.     wl, line = [], line[:-1]
  23861.     while 1:
  23862.         t = pat.exec(line)
  23863.         if t:
  23864.             wl.append( line[ :t[0][0] ])
  23865.             line = line[ t[0][1]: ]
  23866.         else:
  23867.             break
  23868.     if len(fl) > x and len(fl) > y:
  23869.         fl[x], fl[y] = fl[y], fl[x]
  23870.         buf = ''
  23871.         for i in range(0, len(fl)):
  23872.             buf = buf + wl[i] + fl[i]
  23873.         print buf
  23874. -- 
  23875. Lou Kates, louk@research.teleride.on.ca
  23876. 
  23877. 
  23878. Received: from uunet.ca by charon.cwi.nl with SMTP
  23879.     id AA25374 (5.65b/2.10/CWI-Amsterdam); Mon, 14 Sep 1992 20:28:08 +0200
  23880. Received: from tslwat by mail.uunet.ca with UUCP id <9818>; Mon, 14 Sep 1992 14:27:54 -0400
  23881. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23882.     id <m0mU2xa-0000nuC@research.teleride.on.ca>; Sun, 13 Sep 92 19:02 EDT
  23883. Message-Id: <m0mU2xa-0000nuC@research.teleride.on.ca>
  23884. Date:     Sun, 13 Sep 1992 19:02:00 -0400
  23885. From: louk@research.teleride.on.ca (Lou Kates)
  23886. To: python-list@cwi.nl
  23887. Subject: more on awk vs. python example
  23888.  
  23889.  
  23890.  
  23891.  
  23892. This is  a  reworked Python   vs. awk (actually nawk)  comparison
  23893. using the  improvement  suggested by Guido. (Note that  I have an
  23894. older  version  of  Python  and  the exec function in the program
  23895. below is now apparently  called match in more recent versions  of
  23896. Python). The Python program is now  only 50% larger  than the awk
  23897. program and the part that does the real work is actually smaller.
  23898.  
  23899. The size   advantage  of awk   comes entirely from its  automatic
  23900. option processing, argument processing, input loop processing and
  23901. field splitting. The Python algorithm actually beats  the awk one
  23902. in the portion that does the real work.
  23903.  
  23904. The following is the breakdown:
  23905.  
  23906.                 Python        Awk
  23907. Declaration statements        1 stmt        0 stmts
  23908. Default options            1 stmt          1 stmt
  23909. Option processing        3 stmts        0 stmts
  23910. Argument processing        3 stmts        0 stmts
  23911. Reading & looping over input    3 stmts        0 stmts
  23912. Field splitting            1 stmt        0 stmts
  23913.  
  23914. Get list of whitespace fields    0 stmts     3 stmts
  23915. Check number of fields        1 stmt        1 stmt
  23916. Swap & print fields            6 stmts        6 stmts (4 lines)
  23917. End of block stmts        0 stmts        1 stmt
  23918.  
  23919. Total                18 stmt        12 stmts (10 lines)
  23920.  
  23921. Also to  be mentioned is that  the the Python approach eliminates
  23922. the loop  over  the  arguments which  is nice  (although it  does
  23923. require  an  explicit  loop  over  the input lines which awk does
  23924. not).
  23925.  
  23926. On the other hand, the line:
  23927.  
  23928.     line = line[:a] + line[c:d] + line[b:c] + line[a:b] + line[d:]
  23929.  
  23930. in the Python  program is a bit  difficult to follow and what one
  23931. really wants to  say  (to be closer  to how you might think  when
  23932. developing pseudocode) is:
  23933.  
  23934.     swap line[a:b] and line[c:d]
  23935.  
  23936.         or
  23937.  
  23938.     line[a:b], line[c:d] = line[c:d], line[a:b]
  23939.  
  23940. but one can't because strings are immutable.
  23941.  
  23942. Extrapolating the  advantages  listed for   awk, we see that  the
  23943. savings as  a percentage of code size would tend to be relatively
  23944. smaller for  larger  programs. It  is likely that  past a certain
  23945. size  Python has smaller code size while under a certain size awk
  23946. does.
  23947.  
  23948. There really  is no  reason that   with flags to allow  automated
  23949. processing   of    arguments,   automatic processing  of options,
  23950. automatic processing of  the input  loop  and    automatic  field
  23951. splitting plus mutuable strings that the Python code could not be
  23952. made to beat the awk code even for programs of this size.
  23953.  
  23954. It would also  be  nice and certainly  could not hurt to have the
  23955. sub and gsub functions of awk in the Python string library.
  23956.  
  23957.  
  23958. === awk program ===
  23959.  
  23960. BEGIN { if (x == 0) x = 1; if (y == 0) y = 2 }
  23961. NF >= x && NF >=y {
  23962.     line = $0;
  23963.     gsub(/[^ \t]+/, ":", line);
  23964.     split(line, s, ":");
  23965.     t = $y; $y = $x; $x = t;
  23966.     for(i=1; i<=NF; i++) 
  23967.         printf("%s%s", s[i], $i);
  23968.     print "";
  23969. }
  23970.  
  23971.  
  23972. === Python program ===
  23973.  
  23974. import getopt, regexp, string, sys
  23975. options, files = getopt.getopt(sys.argv[1:], 'x:y:')
  23976. x, y = 1, 2
  23977. for (a, b) in options:
  23978.     if 'x' in a: x = string.atoi(b)
  23979.     if 'y' in a: y = string.atoi(b)
  23980. if x > y: x, y = y, x
  23981. fp = sys.stdin
  23982. if files: fp = open(files[0], 'r')
  23983. while 1:
  23984.     line = fp.readline()
  23985.     if not line: break
  23986.     if len(string.split(line)) >= y:
  23987.         t = (regexp.compile('[ \t]*([^ \t]+)' * y)).exec(line)
  23988.         (a, b), (c, d) = t[x], t[y]
  23989.         line = line[:a] + line[c:d] + line[b:c] + line[a:b] + line[d:]
  23990.         print line[:-1]
  23991. 
  23992. 
  23993. Received: from uunet.ca by charon.cwi.nl with SMTP
  23994.     id AA05779 (5.65b/2.10/CWI-Amsterdam); Sun, 20 Sep 1992 21:41:21 +0200
  23995. Received: by mail.uunet.ca via suspension id <10419>; Sun, 20 Sep 1992 15:11:36 -0400
  23996. Received: from tslwat by mail.uunet.ca with UUCP id <11870>; Sat, 19 Sep 1992 21:49:43 -0400
  23997. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  23998.     id <m0mWGLe-000196C@research.teleride.on.ca>; Sat, 19 Sep 92 21:44 EDT
  23999. Message-Id: <m0mWGLe-000196C@research.teleride.on.ca>
  24000. From: louk@research.teleride.on.ca (Lou Kates)
  24001. Subject: Python OO Examples
  24002. To: python-list@cwi.nl
  24003. Date:     Sat, 19 Sep 1992 21:44:29 -0400
  24004. X-Mailer: ELM [version 2.3 PL11]
  24005.  
  24006. Does anyone have any good examples of object oriented programming in
  24007. Python?  (I already have the Python library, of course.)
  24008.  
  24009. -- 
  24010. Lou Kates, louk@research.teleride.on.ca
  24011. 
  24012. 
  24013. Replied: Tue, 29 Sep 1992 21:37:39 +0100
  24014. Replied: Python Mailing List <python-list@cwi.nl>
  24015. Received: from gateway.sequent.com by charon.cwi.nl with SMTP
  24016.     id AA07314 (5.65b/2.10/CWI-Amsterdam); Tue, 29 Sep 1992 20:33:06 +0100
  24017. Received: from eng2.sequent.com by gateway.sequent.com (5.61/1.34)
  24018.     id AA23808; Tue, 29 Sep 92 12:32:09 -0700
  24019. Received: by eng2.sequent.com (5.65/1.34)
  24020.     id AA14602; Tue, 29 Sep 92 12:32:48 -0700
  24021. Message-Id: <9209291932.AA14602@eng2.sequent.com>
  24022. To: Python Mailing List <python-list@cwi.nl>
  24023. Subject: Why doesn't python die gracefully on an SIGTERM???
  24024. Priority: Normal
  24025. Precedence: first-class
  24026. Organization: Sequent Computer Systems, Inc.
  24027.           Decision Support Group - MailStop: RHE2-358
  24028.           15450 S.W. Koll Parkway
  24029.           Beaverton, OR  97006
  24030. X-Phone: (503) 578-4404
  24031. X-Fax: (503) 578-7569
  24032. X-Uucp: ...!uunet!sequent!jaap
  24033. X-Internet: jaap@sequent.com
  24034. X-Face: C4Cnai$>Eja5I6Vq?(gdN#SXX#`-XgAnmUn&e54sx7@1>q@vkrd_XnH![P>w.:7IJJ;{Bts
  24035.  WJd)u&G!V}0OR?2o5cUgIY}.T{g]PMC=*~]3n_t)S-ZkC(WG}3:#hcA6Oazx:}yc&k,hsF7D},7x>l
  24036.  nyfRjO7$@]fHBN>aC9-M3pKfbYHiy!PWD{_bx~fo})b4tU.;Ao%x[upCI,
  24037. Date: Tue, 29 Sep 92 12:32:47 PDT
  24038. From: Jaap Vermeulen <jaap@sequent.com>
  24039.  
  24040.  
  24041. I wrote some python programs that temporarily modify data outside the
  24042. process (in this case utmp). Normally, that data will be restored to the
  24043. correct state when the program terminates normally or when I interrupt
  24044. the execution of the program (python does a good job of cleaning up).
  24045.  
  24046. However, often I run such a python program in the background. Now the
  24047. program ignores the SIGINT and any other signal will cause a non-graceful
  24048. termination where python does not clean up at all.
  24049.  
  24050. Would it be possible to add at least some functionality to allow for
  24051. graceful termination after the most common signal, SIGTERM?
  24052.  
  24053. Or are there any other plans in this area?
  24054.  
  24055. Thanks,
  24056.  
  24057.     -Jaap-
  24058. --
  24059. Jaap Vermeulen                    +--------------------------+
  24060.                         | Sequent Computer Systems |
  24061.     Internet : jaap@sequent.com        | Beaverton, Oregon       |
  24062.     Uucp     : ...uunet!sequent!jaap    +--------------------------+
  24063. 
  24064. 
  24065. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  24066.     id AA07821 (5.65b/2.10/CWI-Amsterdam); Tue, 29 Sep 1992 21:37:38 +0100
  24067. Received: by voorn.cwi.nl with SMTP; Tue, 29 Sep 1992 20:37:37 GMT
  24068. Message-Id: <9209292037.AA12568@voorn.cwi.nl>
  24069. To: Python Mailing List <python-list@cwi.nl>
  24070. Subject: Re: Why doesn't python die gracefully on an SIGTERM??? 
  24071. In-Reply-To: Your message of "Tue, 29 Sep 1992 12:32:47 MDT."
  24072.              <9209291932.AA14602@eng2.sequent.com> 
  24073. From: Guido van Rossum <Guido.van.Rossum@cwi.nl>
  24074. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24075. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24076. Date: Tue, 29 Sep 1992 21:37:36 +0100
  24077. Sender: Guido.van.Rossum@cwi.nl
  24078.  
  24079. >Would it be possible to add at least some functionality to allow for
  24080. >graceful termination after the most common signal, SIGTERM?
  24081.  
  24082. This must be one of the most frequently requested features: either
  24083. provide a general signal handling mechanism, or provide at least a way
  24084. to clean up when a signal arrives.  So far I've avoided the issue of
  24085. designing a "safe" signal handling mechanism for Python, because it's
  24086. almost impossible to do right for everybody without adding
  24087. considerable overhead to the interpreter.   Many proposed solutions
  24088. don't work because of the interpreter's structure (e.g., anything
  24089. involving longjmp() back to the main loop is unsafe because it may
  24090. leave large amounts of allocated memory unreachable and leave internal
  24091. data structures in an inconsistent state).  But given the persistence
  24092. of those who ask for some kind of signal handling, I suppose I simply
  24093. have to add *something*.
  24094.  
  24095. First, in 0.9.7 there will be a cleanup feature that's independent of
  24096. try...finally: if you assign a function object to sys.exitfunc, it
  24097. will be called when Python is about to exit.  I suppose it shouldn't
  24098. be too hard to arrange that this is called when SIGTERM (or SIGHUP)
  24099. arrives too.
  24100.  
  24101. There will be a risk when using this feature: some part of the library
  24102. or interpreter may be busy when the signal arrives and not allow
  24103. re-entrance.  A famous example in C is trying to use printf() to print
  24104. a message from a signal handler: this may fail (causing a core dump)
  24105. when the signal arrives while the mainline code of the program is
  24106. already using printf().  Since Python uses C's stdio for its own I/O,
  24107. this may occur in Python as well.  Other risks specific to Python are
  24108. touching mutable global data structures such as lists, which may be
  24109. inconsistent when an update is in progress.  But again a careful
  24110. Python programmer can minimize the risk by restricting the clean-up
  24111. procedure to the absolutely minimum necessary.
  24112.  
  24113. Another idea, harder to implement, but also more generally useful, is
  24114. to add an interface whereby arbitrary signals can be mapped to
  24115. exceptions just like SIGINT is already mapped to KeyboardInterrupt.
  24116. Or perhaps they should be mappable to function calls (and the default
  24117. mapping for SIGINT could be a function that raises KeyboardInterrupt).
  24118. In this case, the actual call to the (Python) handler will be delayed
  24119. slightly so the interpreter can make sure there are no inconsistent
  24120. (internal) data structures to worry about.  This is already done for
  24121. SIGINT: the actual SIGINT handler just sets a global flag, which is
  24122. checked every few "virtual instructions".
  24123.  
  24124. There is a problem with blocking system calls like read() that needs
  24125. to be fixed in a more general way, too, if this solution is adopted:
  24126. some UNIX systems restart system calls that are interrupted by a
  24127. signal handler.  This would mean that the Python handler would be
  24128. called only after the call returns, which may be never (e.g. when
  24129. reading from a socket or tty).  The current solution, adopted for a
  24130. few important blocking calls only (sleep, read), is to temporarily
  24131. change the SIGINT handler to one that calls longjmp() to a safe point
  24132. in the Python wrapper.  This has to be extended to all caught signals
  24133. and all (or at least most) blocking calls, else the signal mechanism
  24134. would not be reliable.  I'll have to think about this long and deep
  24135. before I proceed to implement it...
  24136.  
  24137. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24138. "I think you have not fully appreciated our attitude towards the tenants"
  24139. 
  24140. 
  24141. To: Python Mailing List <python-list@cwi.nl>
  24142. Subject: Re: Why doesn't python die gracefully on an SIGTERM??? 
  24143. In-reply-to: Your message of "Tue, 29 Sep 1992 12:32:47 MDT."
  24144.              <9209291932.AA14602@eng2.sequent.com> 
  24145. From: Guido van Rossum <guido@cwi.nl>
  24146. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24147. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24148. Date: Tue, 29 Sep 1992 21:37:36 +0100
  24149. Sender: guido
  24150.  
  24151. >Would it be possible to add at least some functionality to allow for
  24152. >graceful termination after the most common signal, SIGTERM?
  24153.  
  24154. This must be one of the most frequently requested features: either
  24155. provide a general signal handling mechanism, or provide at least a way
  24156. to clean up when a signal arrives.  So far I've avoided the issue of
  24157. designing a "safe" signal handling mechanism for Python, because it's
  24158. almost impossible to do right for everybody without adding
  24159. considerable overhead to the interpreter.   Many proposed solutions
  24160. don't work because of the interpreter's structure (e.g., anything
  24161. involving longjmp() back to the main loop is unsafe because it may
  24162. leave large amounts of allocated memory unreachable and leave internal
  24163. data structures in an inconsistent state).  But given the persistence
  24164. of those who ask for some kind of signal handling, I suppose I simply
  24165. have to add *something*.
  24166.  
  24167. First, in 0.9.7 there will be a cleanup feature that's independent of
  24168. try...finally: if you assign a function object to sys.exitfunc, it
  24169. will be called when Python is about to exit.  I suppose it shouldn't
  24170. be too hard to arrange that this is called when SIGTERM (or SIGHUP)
  24171. arrives too.
  24172.  
  24173. There will be a risk when using this feature: some part of the library
  24174. or interpreter may be busy when the signal arrives and not allow
  24175. re-entrance.  A famous example in C is trying to use printf() to print
  24176. a message from a signal handler: this may fail (causing a core dump)
  24177. when the signal arrives while the mainline code of the program is
  24178. already using printf().  Since Python uses C's stdio for its own I/O,
  24179. this may occur in Python as well.  Other risks specific to Python are
  24180. touching mutable global data structures such as lists, which may be
  24181. inconsistent when an update is in progress.  But again a careful
  24182. Python programmer can minimize the risk by restricting the clean-up
  24183. procedure to the absolutely minimum necessary.
  24184.  
  24185. Another idea, harder to implement, but also more generally useful, is
  24186. to add an interface whereby arbitrary signals can be mapped to
  24187. exceptions just like SIGINT is already mapped to KeyboardInterrupt.
  24188. Or perhaps they should be mappable to function calls (and the default
  24189. mapping for SIGINT could be a function that raises KeyboardInterrupt).
  24190. In this case, the actual call to the (Python) handler will be delayed
  24191. slightly so the interpreter can make sure there are no inconsistent
  24192. (internal) data structures to worry about.  This is already done for
  24193. SIGINT: the actual SIGINT handler just sets a global flag, which is
  24194. checked every few "virtual instructions".
  24195.  
  24196. There is a problem with blocking system calls like read() that needs
  24197. to be fixed in a more general way, too, if this solution is adopted:
  24198. some UNIX systems restart system calls that are interrupted by a
  24199. signal handler.  This would mean that the Python handler would be
  24200. called only after the call returns, which may be never (e.g. when
  24201. reading from a socket or tty).  The current solution, adopted for a
  24202. few important blocking calls only (sleep, read), is to temporarily
  24203. change the SIGINT handler to one that calls longjmp() to a safe point
  24204. in the Python wrapper.  This has to be extended to all caught signals
  24205. and all (or at least most) blocking calls, else the signal mechanism
  24206. would not be reliable.  I'll have to think about this long and deep
  24207. before I proceed to implement it...
  24208.  
  24209. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24210. "I think you have not fully appreciated our attitude towards the tenants"
  24211. 
  24212. 
  24213. Replied: Thu, 08 Oct 1992 00:30:31 +0100
  24214. Replied: python-list@cwi.nl
  24215. Received: from mcsun.EU.net by charon.cwi.nl with SMTP
  24216.     id AA15607 (5.65b/2.10/CWI-Amsterdam); Thu, 8 Oct 1992 00:10:19 +0100
  24217. Received: by mcsun.EU.net via EUnet
  24218.     id AA01700 (5.65b/CWI-2.179); Thu, 8 Oct 1992 00:10:17 +0100
  24219. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay1.UU.NET with SMTP 
  24220.     (5.61/UUNET-internet-primary) id AA05489; Wed, 7 Oct 92 19:04:08 -0400
  24221. Received: from decwrl.UUCP by uunet.uu.net with UUCP/RMAIL
  24222.     (queueing-rmail) id 190356.5428; Wed, 7 Oct 1992 19:03:56 EDT
  24223. Received: by uucp-gw-1.pa.dec.com; id AA07545; Wed, 7 Oct 92 15:56:02 -0700
  24224. Received: from  lancelot.atherton.com  (lancelot) by eng.atherton.com (3.2/smail2.2/05-26-87)id AA17208; Wed, 7 Oct 92 15:35:12 PDT
  24225. Received: by  lancelot.atherton.com  (NeXT-1.0 (From Sendmail 5.52)/NeXT-2.0)id AA01220; Wed, 7 Oct 92 15:27:07 PDT
  24226. Date: Wed, 7 Oct 92 15:27:07 PDT
  24227. From: scottr@atherton.com (Scott F. Rasmussen)
  24228. Message-Id: <9210072227.AA01220@ lancelot.atherton.com >
  24229. Received: by NeXT Mailer (1.63.RR)
  24230. To: python-list@cwi.nl
  24231. Subject: Need info on integrating python into my application
  24232.  
  24233. Hello everyone,
  24234.  
  24235. I have recently acquired python and trying to understand how to integrate python in with my  
  24236. application.  I have looked through all the documentation but am still mystified by one question.  How  
  24237. do I call a python function from C?  This is the typical callback situation.  
  24238.  
  24239. In the callback I want to execute the following bit of python code.
  24240.  
  24241. def callback (args):
  24242.     stuff ....
  24243.     
  24244. In C how do I call the callback function?  The question is how do I get a handle to the function  
  24245. callback so that I can call it.
  24246.  
  24247. Thank you for any help...
  24248.  
  24249. ---
  24250.  
  24251. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=
  24252. Scott Rasmussen                                    scottr@atherton.com
  24253. Atherton Technology                           {sun|decwrl}!athertn.com
  24254. 
  24255. 
  24256. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  24257.     id AA17135 (5.65b/2.10/CWI-Amsterdam); Thu, 8 Oct 1992 00:30:31 +0100
  24258. Received: by voorn.cwi.nl with SMTP; Wed, 7 Oct 1992 23:30:30 GMT
  24259. Message-Id: <9210072330.AA03863@voorn.cwi.nl>
  24260. To: python-list@cwi.nl
  24261. Subject: Re: Need info on integrating python into my application 
  24262. In-Reply-To: Your message of "Wed, 07 Oct 1992 15:27:07 MDT."
  24263.              <9210072227.AA01220@ lancelot.atherton.com > 
  24264. From: Guido.van.Rossum@cwi.nl
  24265. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24266. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24267. Date: Thu, 08 Oct 1992 00:30:29 +0100
  24268. Sender: Guido.van.Rossum@cwi.nl
  24269.  
  24270. >I have recently acquired python and trying to understand how to
  24271. >integrate python in with my   
  24272. >application.  I have looked through all the documentation but am
  24273. >still mystified by one question.  How   
  24274. >do I call a python function from C?  This is the typical callback situation.  
  24275.  
  24276. If you have python 0.9.6 (and there's no reason not to have it), the
  24277. file misc/EXTENDING (the place to start if you want to integrate
  24278. Python and C code even if you don't want to add new modules) has a
  24279. section on calling a Python function from C.  The essential thing to
  24280. know is to use the function call_object(funcobj, argobj) and to
  24281. include <ceval.h> for its declaration (I think -- it's changed a bit
  24282. in the almost-0.9.7 version I'm currently using myself...).
  24283.  
  24284. >In the callback I want to execute the following bit of python code.
  24285. >
  24286. >def callback (args):
  24287. >    stuff ....
  24288. >    
  24289. >In C how do I call the callback function?  The question is how do I
  24290. >get a handle to the function   
  24291. >callback so that I can call it.
  24292.  
  24293. You call it using call_object().  To get a "handle" to the function,
  24294. you need a function object.  Most existing modules written in C that
  24295. use callbacks have some entry point with which the Python user
  24296. passes the callback function to the module, e.g. in the above example
  24297. there might be an external module "my_extension" with a function
  24298. "call_this_later" and the user might call it as follows (assuming the
  24299. above definition of callback():
  24300.  
  24301.       >>> my_extension.call_this_later(callback)
  24302.  
  24303. Cheers,
  24304.  
  24305. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24306. "Many of my best friends are lumberjacks and only a few of them are
  24307. transvestites"
  24308. 
  24309. 
  24310. To: python-list@cwi.nl
  24311. Subject: Re: Need info on integrating python into my application 
  24312. In-reply-to: Your message of "Wed, 07 Oct 1992 15:27:07 MDT."
  24313.              <9210072227.AA01220@ lancelot.atherton.com > 
  24314. From: Guido.van.Rossum@CWI.NL
  24315. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24316. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24317. Date: Thu, 08 Oct 1992 00:30:29 +0100
  24318. Sender: guido
  24319.  
  24320. >I have recently acquired python and trying to understand how to
  24321. >integrate python in with my   
  24322. >application.  I have looked through all the documentation but am
  24323. >still mystified by one question.  How   
  24324. >do I call a python function from C?  This is the typical callback situation.  
  24325.  
  24326. If you have python 0.9.6 (and there's no reason not to have it), the
  24327. file misc/EXTENDING (the place to start if you want to integrate
  24328. Python and C code even if you don't want to add new modules) has a
  24329. section on calling a Python function from C.  The essential thing to
  24330. know is to use the function call_object(funcobj, argobj) and to
  24331. include <ceval.h> for its declaration (I think -- it's changed a bit
  24332. in the almost-0.9.7 version I'm currently using myself...).
  24333.  
  24334. >In the callback I want to execute the following bit of python code.
  24335. >
  24336. >def callback (args):
  24337. >    stuff ....
  24338. >    
  24339. >In C how do I call the callback function?  The question is how do I
  24340. >get a handle to the function   
  24341. >callback so that I can call it.
  24342.  
  24343. You call it using call_object().  To get a "handle" to the function,
  24344. you need a function object.  Most existing modules written in C that
  24345. use callbacks have some entry point with which the Python user
  24346. passes the callback function to the module, e.g. in the above example
  24347. there might be an external module "my_extension" with a function
  24348. "call_this_later" and the user might call it as follows (assuming the
  24349. above definition of callback():
  24350.  
  24351.       >>> my_extension.call_this_later(callback)
  24352.  
  24353. Cheers,
  24354.  
  24355. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24356. "Many of my best friends are lumberjacks and only a few of them are
  24357. transvestites"
  24358. 
  24359. 
  24360. Replied: Sun, 13 Dec 1992 20:56:09 +0100
  24361. Replied: "Fraser@europarc.xerox.com "
  24362. Replied: Mon, 16 Nov 1992 15:06:52 +0100
  24363. Replied: "python-list "
  24364. Received: from alpha.Xerox.COM by charon.cwi.nl with SMTP
  24365.     id AA19776 (5.65b/3.2/CWI-Amsterdam); Fri, 13 Nov 1992 17:11:37 +0100
  24366. Received: from aphrodite.EuroPARC.Xerox.COM ([13.1.252.142]) by alpha.xerox.com with SMTP id <12060>; Fri, 13 Nov 1992 08:11:15 PST
  24367. Received: by aphrodite.EuroPARC.Xerox.COM with SMTP
  24368.     (5.65c/IDA-1.2.9) id AA24783; Fri, 13 Nov 1992 16:09:29 GMT
  24369. Message-Id: <199211131609.AA24783@aphrodite.EuroPARC.Xerox.COM>
  24370. To: python-list@cwi.nl
  24371. Subject: Ooops - sorry
  24372. Date:     Fri, 13 Nov 1992 08:09:27 PST
  24373. From: Fraser@europarc.xerox.com
  24374.  
  24375.  
  24376. Apologies for my silly message!
  24377.  
  24378. Firstly, many thanks for a wonderful product - I discovered python a few
  24379. days ago and haven't stopped playing since... :-)
  24380.  
  24381. I'm new to python, so forgive me if these are questions to which I should
  24382. know the answers...
  24383.  
  24384. 1) Is there a digest of past messages?
  24385.  
  24386. 2) Now that we have dynamic linking, has anybody done any stub generation
  24387. for Sun RPC?
  24388.  
  24389. Quentin
  24390. 
  24391. 
  24392. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  24393.     id AA11361 (5.65b/3.2/CWI-Amsterdam); Mon, 16 Nov 1992 15:06:53 +0100
  24394. Received: by voorn.cwi.nl with SMTP
  24395.     id AA13720 (5.65b/3.2/CWI-Amsterdam); Mon, 16 Nov 1992 15:06:52 +0100
  24396. Message-Id: <9211161406.AA13720.guido@voorn.cwi.nl>
  24397. To: python-list@cwi.nl
  24398. Subject: Re: Questions from Fraser@europarc.xerox.com
  24399. In-Reply-To: Your message of "Fri, 13 Nov 1992 08:09:27 MET."
  24400.              <199211131609.AA24783@aphrodite.EuroPARC.Xerox.COM> 
  24401. From: Guido.van.Rossum@cwi.nl
  24402. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24403. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24404. Date: Mon, 16 Nov 1992 15:06:51 +0100
  24405. Sender: Guido.van.Rossum@cwi.nl
  24406.  
  24407. >1) Is there a digest of past messages?
  24408.  
  24409. Not formally, but I have saved (almost) every message to the list
  24410. since November 1991 in an MH folder (i.e. one file per message).  I
  24411. can make this available by anonymous ftp.  Do people have preferences
  24412. for the format I should use?  I could make a tar file of the entire
  24413. folder, just copy all the files, or perhaps some fancy digest format.
  24414. Easiest to update for me would be a copy of the entire folder (I would
  24415. use rdist to add recent messages) but I can imagine this would be hard
  24416. to use.  If you have a preference, speak up!
  24417.  
  24418. >2) Now that we have dynamic linking, has anybody done any stub generation
  24419. >for Sun RPC?
  24420.  
  24421. Not me, sorry.
  24422.  
  24423. BTW if people who write useful Python code could mail it to me or at
  24424. least tell me about its existence, I can compile a catalog.  If you do
  24425. this within a week or two the catalog can be part of the next release,
  24426. 0.9.8 (I'm going to skip 0.9.7 since a lot has changed since 0.9.7beta).
  24427.  
  24428. Cheers all,
  24429.  
  24430. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24431. "Oh mother, don't be so sentimental.  Things explode every day."
  24432. 
  24433. 
  24434. Received: from uunet.ca by charon.cwi.nl with SMTP
  24435.     id AA17614 (5.65b/3.2/CWI-Amsterdam); Mon, 16 Nov 1992 18:26:29 +0100
  24436. Received: from tslwat by mail.uunet.ca with UUCP id <9861>; Mon, 16 Nov 1992 12:26:16 -0500
  24437. Received: by research.teleride.on.ca (/\==/\ Smail3.1.25.1 #25.2)
  24438.     id <m0mr8xd-0001HDC@research.teleride.on.ca>; Mon, 16 Nov 92 11:06 EST
  24439. Message-Id: <m0mr8xd-0001HDC@research.teleride.on.ca>
  24440. From: louk@research.teleride.on.ca (Lou Kates)
  24441. Subject: Re: Questions from Fraser@europarc.xerox.com
  24442. To: Guido.van.Rossum@cwi.nl
  24443. Date:     Mon, 16 Nov 1992 11:06:00 -0500
  24444. In-Reply-To: <9211161406.AA13720.guido@voorn.cwi.nl>; from "Guido.van.Rossum@cwi.nl" at Nov 16, 92 9:06 am
  24445. X-Mailer: ELM [version 2.3 PL11]
  24446.  
  24447. >
  24448. > BTW if people who write useful Python code could mail it to me or at
  24449. > least tell me about its existence, I can compile a catalog.  If you do
  24450. > this within a week or two the catalog can be part of the next release,
  24451. > 0.9.8 (I'm going to skip 0.9.7 since a lot has changed since 0.9.7beta).
  24452. >
  24453.  
  24454. The   following    is  short,    uses  several python  libraries,
  24455. illustrates writing a  filter   and using  flags so its good  for
  24456. learning Python. I think you may have changed  the regexp library
  24457. since my copy of Python so we might  want to  change the function
  24458. names appropriately.
  24459.  
  24460. Lou Kates, louk@research.teleride.on.ca
  24461.  
  24462.  
  24463. # swap fields x and y preserving whitespace; skip lines with too few fields
  24464. # eg:     python swapcols.py -x 2 -y 3 myfile.dat
  24465.  
  24466. import getopt, regexp, string, sys
  24467.  
  24468. options, files = getopt.getopt(sys.argv[1:], 'x:y:')
  24469.  
  24470. x, y = 1, 2
  24471. for (flag, value) in options:
  24472.         if 'x' in flag: x = string.atoi(value)
  24473.         if 'y' in flag: y = string.atoi(value)
  24474. if x > y: x, y = y, x
  24475.  
  24476. fp = sys.stdin
  24477. if files: fp = open(files[0], 'r')
  24478.  
  24479. while 1:
  24480.         line = fp.readline()
  24481.         if not line: break
  24482.         if len(string.split(line)) >= y:
  24483.                 t = (regexp.compile('[ \t]*([^ \t]+)' * y)).exec(line)
  24484.                 (a, b), (c, d) = t[x], t[y]
  24485.                 line = line[:a] + line[c:d] + line[b:c] + line[a:b] + line[d:]
  24486.                 print line[:-1]
  24487. 
  24488. 
  24489. To: python-list@cwi.nl
  24490. cc: Tim Berners-Lee <timbl@www3.cern.ch>
  24491. Subject: Python library documentation on-line
  24492. From: Guido.van.Rossum@cwi.nl
  24493. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24494. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24495. Date: Thu, 19 Nov 1992 18:11:45 +0100
  24496. Sender: guido
  24497.  
  24498. Thanks to Jan-Hein Buhrman, the library documentation for Python can
  24499. now be browsed on-line in two ways: as Emacs INFO tree (1) and as a
  24500. bunch of WWW hypertext nodes (2).
  24501.  
  24502. THANK YOU, JAN-HEIN!
  24503.  
  24504. (1) Emacs INFO tree
  24505.  
  24506. The library documentation is available in the format understood by the
  24507. Emacs INFO browser.  For this you need to fetch the tar file
  24508. python-lib-info.tar from anonymous ftp site ftp.cwi.nl
  24509. [192.16.184.180], directory /pub.
  24510.  
  24511. The tar file contains 6 compressed info files which together comprise
  24512. the entire info tree for the library manual.  You can go to the top of
  24513. the tree using the 'g' command in INFO mode, type g followed by
  24514. (python-lib), assuming the tar file has been extracted in the right
  24515. directory.  Of course you can also edit your "localdir" file.
  24516.  
  24517. You don't need Emacs to browse INFO files: if you uncompress the
  24518. info files, they can also be browsed with the info browser written in
  24519. Python from the Python distribution's demo directory,
  24520. python/demo/stdwin/ibrowse/ib.  (This requires that you have installed
  24521. the STDWIN extension to Python.)
  24522.  
  24523. (2) WWW hypertext
  24524.  
  24525. If you have a browser from CERN's WWW project (World-Wide Web, a
  24526. distributed hypertext system) you can browse a WWW hypertext version
  24527. of the manual by following the following link:
  24528.  
  24529.     http://voorn.cwi.nl:2784/texinfo/python/top.html
  24530.  
  24531. If your name server doesn't know about voorn.cwi.nl, substitute
  24532. 192.16.201.121.  Try http://voorn.cwi.nl:2784/default.html to see
  24533. what other WWW documents are available from CWI -- I have converted
  24534. several other texinfo documents to WWW's format and made them
  24535. available, such as the Emacs manual.
  24536.  
  24537. It is possible that our Internet interface prohibits access to port
  24538. 2784 on voorn.cwi.nl.  In that case, you can try the following slower
  24539. but equivalent access path using ftp:
  24540.  
  24541.     file://ftp.cwi.nl/pub/www/texinfo/python/top.html
  24542.  
  24543. Sources and binaries of WWW browsers can be ftp'ed from site
  24544. ftp.cern.ch, directory pub/www.  Start extracting README.txt.  The
  24545. Viola browser is quite worthwile (if you have X11).
  24546.  
  24547. I have also written a WWW browser in Python, but its displayer and
  24548. parser are rather slow compared to the C versions.  If you're
  24549. interested in the texinfo-to-WWW conversion program (also in Python),
  24550. send me mail.
  24551.  
  24552. Next projects: converting the reference manual and the tutorial...
  24553.  
  24554. Cheers,
  24555.  
  24556. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24557. "Now, are you going to give me a fish licence?"
  24558. 
  24559. 
  24560. Replied: Sun, 06 Dec 1992 15:13:50 +0100
  24561. Replied: "Lance Ellinghouse <lance@markv.com> python-list@cwi.nl"
  24562. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24563.     id AA16429 (5.65b/3.2/CWI-Amsterdam); Tue, 1 Dec 1992 20:02:29 +0100
  24564. From: Lance Ellinghouse <lance@markv.com>
  24565. X-Mailer: SCO System V Mail (version 3.2)
  24566. To: python-list@cwi.nl
  24567. Subject: Curses Interface
  24568. Date: Tue, 1 Dec 92 11:01:37 PST
  24569. Message-Id:  <9212011101.aa28875@hermix.markv.com>
  24570.  
  24571. Has anyone created a Curses interface layer (similar to STDWIN) for Python
  24572. yet?
  24573.  
  24574. Lance Ellinghouse
  24575. lance@markv.com
  24576. 
  24577. 
  24578. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24579.     id AA22246 (5.65b/3.2/CWI-Amsterdam); Tue, 1 Dec 1992 23:10:19 +0100
  24580. From: Lance Ellinghouse <lance@markv.com>
  24581. X-Mailer: SCO System V Mail (version 3.2)
  24582. To: python-list@cwi.nl
  24583. Subject: additional modules/objects
  24584. Date: Tue, 1 Dec 92 14:09:15 PST
  24585. Message-Id:  <9212011409.aa09783@hermix.markv.com>
  24586.  
  24587. Is "ftp.cwi.nl" the only place where Python modules/objects/scripts
  24588. are??? Does anyone else have any that are available?
  24589.  
  24590. Lance
  24591. 
  24592. 
  24593. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  24594.     id AA16635 (5.65b/3.2/CWI-Amsterdam); Wed, 2 Dec 1992 10:54:32 +0100
  24595. Received: by schelvis.cwi.nl with SMTP
  24596.     id AA26559 (5.65b/3.2/CWI-Amsterdam); Wed, 2 Dec 1992 10:38:52 +0100
  24597. Message-Id: <9212020938.AA26559.jack@schelvis.cwi.nl>
  24598. To: Lance Ellinghouse <lance@markv.com>
  24599. Cc: python-list@cwi.nl
  24600. Subject: Re: additional modules/objects 
  24601. In-Reply-To: Message by Lance Ellinghouse <lance@markv.com> ,
  24602.          Tue, 1 Dec 92 14:09:15 PST , <9212011409.aa09783@hermix.markv.com> 
  24603. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  24604. Phone: +31 20 5924098(work), +31 20 5924199 (fax), +31 20 6160335(home)
  24605. X-Last-Band-Seen: Alice Donut (Melkweg, 28-11)
  24606. X-Mini-Review: Good.
  24607. Date: Wed, 02 Dec 1992 10:38:52 +0100
  24608. From: Jack Jansen <Jack.Jansen@cwi.nl>
  24609.  
  24610.  
  24611. Recently, Lance Ellinghouse <lance@markv.com> said:
  24612.     Is "ftp.cwi.nl" the only place where Python modules/objects/scripts
  24613.     are??? Does anyone else have any that are available?
  24614.     
  24615.     Lance
  24616.  
  24617. If anyone has available modules that they feel would be of general
  24618. interest please also tell Guido, so he can include them in the distribution.
  24619. --
  24620. Jack Jansen        | If I can't dance I don't want to be part of
  24621. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  24622. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  24623. 
  24624. 
  24625. Replied: Sun, 06 Dec 1992 14:51:39 +0100
  24626. Replied: "Fraser@europarc.xerox.com python-list"
  24627. Received: from alpha.Xerox.COM by charon.cwi.nl with SMTP
  24628.     id AA18976 (5.65b/3.2/CWI-Amsterdam); Wed, 2 Dec 1992 12:07:17 +0100
  24629. Received: from aphrodite.EuroPARC.Xerox.COM ([13.1.252.142]) by alpha.xerox.com with SMTP id <11888>; Wed, 2 Dec 1992 03:06:59 PST
  24630. Received: by aphrodite.EuroPARC.Xerox.COM with SMTP
  24631.     (5.65c/IDA-1.2.9) id AA02797; Wed, 2 Dec 1992 11:05:09 GMT
  24632. Message-Id: <199212021105.AA02797@aphrodite.EuroPARC.Xerox.COM>
  24633. To: python-list@cwi.nl
  24634. Subject: Re: Curses Interface 
  24635. In-Reply-To: Your message of "Tue, 01 Dec 92 11:01:37 PST."
  24636.              <9212011101.aa28875@hermix.markv.com> 
  24637. Date:     Wed, 2 Dec 1992 03:05:01 PST
  24638. From: Fraser@europarc.xerox.com
  24639.  
  24640.  
  24641. > Has anyone created a Curses interface layer (similar to STDWIN) for 
  24642. > Python yet?
  24643.  
  24644. I started to write a module for a subset of curses, called curselet, but
  24645. ran into problems with the dynamic loader. Can anyone suggest why the dl
  24646. system fails to find any of the symbols in my /usr/lib/libcurses.a? My
  24647. curseletmodule.libs contains just:
  24648.  
  24649. /usr/lib/libcurses.a /usr/lib/libc.a
  24650.  
  24651. and it finds things in libc.a, but I get :
  24652.  
  24653. dl_loadmod: dld_link of library failed: 10
  24654. dl_loadmod: 4 undefined symbols remain
  24655. _endwin
  24656. _wrefresh
  24657. _stdscr
  24658. _initscr
  24659. dl_loadmod: function initcurselet not executable
  24660.  
  24661. when I try and import curselet.
  24662.  
  24663. Thanks in advance.
  24664.  
  24665. Quentin
  24666.  
  24667. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24668. Quentin Stafford-Fraser        Tel: +44 223 341529
  24669. Rank Xerox EuroPARC,        Fax: +44 223 341510
  24670. Cambridge, UK            Fraser@europarc.xerox.com
  24671.  
  24672. 
  24673. 
  24674. Received: from schelvis.cwi.nl by charon.cwi.nl with SMTP
  24675.     id AA27738 (5.65b/3.2/CWI-Amsterdam); Wed, 2 Dec 1992 17:02:06 +0100
  24676. Received: by schelvis.cwi.nl with SMTP
  24677.     id AA27552 (5.65b/3.2/CWI-Amsterdam); Wed, 2 Dec 1992 17:02:05 +0100
  24678. Message-Id: <9212021602.AA27552.jack@schelvis.cwi.nl>
  24679. To: Fraser@europarc.xerox.com
  24680. Cc: python-list@cwi.nl
  24681. Subject: Re: Curses Interface 
  24682. In-Reply-To: Message by Fraser@europarc.xerox.com ,
  24683.          Wed, 2 Dec 1992 03:05:01 PST , <199212021105.AA02797@aphrodite.EuroPARC.Xerox.COM> 
  24684. Organisation: Multi-media group, CWI, Kruislaan 413, Amsterdam
  24685. Phone: +31 20 5924098(work), +31 20 5924199 (fax), +31 20 6160335(home)
  24686. X-Last-Band-Seen: Alice Donut (Melkweg, 28-11)
  24687. X-Mini-Review: Good.
  24688. Date: Wed, 02 Dec 1992 17:02:04 +0100
  24689. From: Jack Jansen <Jack.Jansen@cwi.nl>
  24690.  
  24691. Hmm. You didn't mention what system you are using, but it is obviously
  24692. not an SGI, since dld is used for dynamic linking. A Sun?
  24693.  
  24694. I'd look at the two libraries (with 'file', for instance) to see if
  24695. they are compiled differently. On some systems libraries have to be
  24696. compiled in a funny way to allow them to work with dynamic loading.
  24697. Maybe your system admin can provide some help here.
  24698. --
  24699. Jack Jansen        | If I can't dance I don't want to be part of
  24700. Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
  24701. uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl
  24702. 
  24703. 
  24704. Replied: Sun, 06 Dec 1992 15:05:15 +0100
  24705. Replied: "Lance Ellinghouse <lance@markv.com> python-list"
  24706. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24707.     id AA22860 (5.65b/3.2/CWI-Amsterdam); Thu, 3 Dec 1992 20:51:44 +0100
  24708. From: Lance Ellinghouse <lance@markv.com>
  24709. X-Mailer: SCO System V Mail (version 3.2)
  24710. To: python-list@cwi.nl
  24711. Subject: nice() in the os module?
  24712. Date: Thu, 3 Dec 92 11:50:33 PST
  24713. Message-Id:  <9212031150.aa13749@hermix.markv.com>
  24714.  
  24715. Why is there no 'nice()' function in the 'os' module?
  24716.  
  24717. Lance Ellinghouse
  24718. lance@markv.com
  24719. 
  24720. 
  24721. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24722.     id AA25311 (5.65b/3.2/CWI-Amsterdam); Thu, 3 Dec 1992 23:02:05 +0100
  24723. From: Lance Ellinghouse <lance@markv.com>
  24724. X-Mailer: SCO System V Mail (version 3.2)
  24725. To: python-list@cwi.nl
  24726. Subject: DOS version of Python 0.9.7beta
  24727. Date: Thu, 3 Dec 92 14:01:18 PST
  24728. Message-Id:  <9212031401.aa18760@hermix.markv.com>
  24729.  
  24730. Does anyone have Python 0.9.7beta compiled for MS-DOS?????
  24731.  
  24732. Thanks, 
  24733. Lance Ellinghouse
  24734. lance@markv.com
  24735. 
  24736. 
  24737. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  24738.     id AA20841 (5.65b/3.2/CWI-Amsterdam); Sun, 6 Dec 1992 14:51:40 +0100
  24739. Received: by voorn.cwi.nl with SMTP
  24740.     id AA16027 (5.65b/3.2/CWI-Amsterdam); Sun, 6 Dec 1992 14:51:39 +0100
  24741. Message-Id: <9212061351.AA16027.guido@voorn.cwi.nl>
  24742. To: Fraser@europarc.xerox.com
  24743. Cc: python-list@cwi.nl
  24744. Subject: Re: Curses Interface 
  24745. In-Reply-To: Your message of "Wed, 02 Dec 1992 03:05:01 MET."
  24746.              <199212021105.AA02797@aphrodite.EuroPARC.Xerox.COM> 
  24747. From: Guido.van.Rossum@cwi.nl
  24748. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24749. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24750. Date: Sun, 06 Dec 1992 14:51:39 +0100
  24751. Sender: Guido.van.Rossum@cwi.nl
  24752.  
  24753. >I started to write a module for a subset of curses, called curselet, but
  24754. >ran into problems with the dynamic loader. Can anyone suggest why the dl
  24755. >system fails to find any of the symbols in my /usr/lib/libcurses.a? My
  24756. >curseletmodule.libs contains just:
  24757. >
  24758. >/usr/lib/libcurses.a /usr/lib/libc.a
  24759. >
  24760. >and it finds things in libc.a, but I get :
  24761. >
  24762. >dl_loadmod: dld_link of library failed: 10
  24763. >dl_loadmod: 4 undefined symbols remain
  24764. >_endwin
  24765. >_wrefresh
  24766. >_stdscr
  24767. >_initscr
  24768. >dl_loadmod: function initcurselet not executable
  24769. >
  24770. >when I try and import curselet.
  24771.  
  24772. Hm, shouldn't you be using /usr/lib/libcurses_G0.a /usr/lib/libc_G0.a?
  24773. (Everything to be loaded dynamically has to be compiled with -G0 on
  24774. the SGI; SGI provides versions of some libraries compiled with -G0.)
  24775.  
  24776. Hope this helps,
  24777.  
  24778. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24779. "Afterlife, sir?  No, sir."
  24780. 
  24781. 
  24782. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  24783.     id AA21582 (5.65b/3.2/CWI-Amsterdam); Sun, 6 Dec 1992 15:05:17 +0100
  24784. Received: by voorn.cwi.nl with SMTP
  24785.     id AA16068 (5.65b/3.2/CWI-Amsterdam); Sun, 6 Dec 1992 15:05:16 +0100
  24786. Message-Id: <9212061405.AA16068.guido@voorn.cwi.nl>
  24787. To: Lance Ellinghouse <lance@markv.com>
  24788. Cc: python-list@cwi.nl
  24789. Subject: Re: nice() in the os module? 
  24790. In-Reply-To: Your message of "Thu, 03 Dec 1992 11:50:33 MET."
  24791.              <9212031150.aa13749@hermix.markv.com> 
  24792. From: Guido.van.Rossum@cwi.nl
  24793. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24794. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24795. Date: Sun, 06 Dec 1992 15:05:15 +0100
  24796. Sender: Guido.van.Rossum@cwi.nl
  24797.  
  24798. >Why is there no 'nice()' function in the 'os' module?
  24799.  
  24800. Because no-one needed it before and I didn't think it would be very
  24801. useful.  After all you can also run a script using the nice(1)
  24802. command.  And, I don't believe this is part of the POSIX standard.
  24803. If you want it you can add it to posixmodule.c with only a few lines.
  24804. If enough people want it I can add it to 0.9.8.
  24805.  
  24806. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24807. "Well there's spam, egg, sausage and spam.  That's not got *much* spam
  24808. in it"
  24809. 
  24810. 
  24811. To: Lance Ellinghouse <lance@markv.com>
  24812. cc: python-list@cwi.nl
  24813. Subject: Re: Curses Interface 
  24814. In-reply-to: Your message of "Tue, 01 Dec 1992 11:01:37 MET."
  24815.              <9212011101.aa28875@hermix.markv.com> 
  24816. From: Guido.van.Rossum@cwi.nl
  24817. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24818. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24819. Date: Sun, 06 Dec 1992 15:13:50 +0100
  24820. Sender: guido
  24821.  
  24822. >Has anyone created a Curses interface layer (similar to STDWIN) for Python
  24823. >yet?
  24824.  
  24825. Note that there is a "curses-like" version of stdwin, where you can
  24826. use the stdwin API (or at least a large subset) and run the program on
  24827. an ASCII terminal.  Look at stdwin/Ports/alfa and vtrm.  You'll have
  24828. to build a separate Python interpreted for this, linked with this
  24829. alternative stdwin library.
  24830.  
  24831. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24832.  
  24833. PS: It seems I replied to quick about the "dl" question: as Jack
  24834. notices, this isn't on an SGI platforms.  Sorry for bothering you
  24835. folks...
  24836. 
  24837. 
  24838. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24839.     id AA01893 (5.65b/3.2/CWI-Amsterdam); Mon, 7 Dec 1992 19:45:10 +0100
  24840. From: Lance Ellinghouse <lance@markv.com>
  24841. X-Mailer: SCO System V Mail (version 3.2)
  24842. To: Guido.van.Rossum@cwi.nl
  24843. Subject: Re: Curses Interface
  24844. Cc: python-list@cwi.nl
  24845. Date: Mon, 7 Dec 92 10:44:04 PST
  24846. Message-Id:  <9212071044.aa28849@hermix.markv.com>
  24847.  
  24848. > >Has anyone created a Curses interface layer (similar to STDWIN) for Python
  24849. > >yet?
  24850.  
  24851. > Note that there is a "curses-like" version of stdwin, where you can
  24852. > use the stdwin API (or at least a large subset) and run the program on
  24853. > an ASCII terminal.  Look at stdwin/Ports/alfa and vtrm.  You'll have
  24854. > to build a separate Python interpreted for this, linked with this
  24855. > alternative stdwin library.
  24856.  
  24857. The problem is that I then have to constantly port another package
  24858. whenever updates come about... If I use the standard CURSES libs, I 
  24859. don't have to worry about it.
  24860.  
  24861. Lance Ellinghouse
  24862. lance@markv.com
  24863.  
  24864. 
  24865. 
  24866. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  24867.     id AA01646 (5.65b/3.2/CWI-Amsterdam); Tue, 15 Dec 1992 12:27:53 +0100
  24868. Received: by voorn.cwi.nl with SMTP
  24869.     id AA09507 (5.65b/3.2/CWI-Amsterdam); Tue, 15 Dec 1992 12:27:50 +0100
  24870. Message-Id: <9212151127.AA09507.guido@voorn.cwi.nl>
  24871. To: python-list@cwi.nl
  24872. Subject: Re: DOS version of Python 
  24873. In-Reply-To: Your message of "Mon, 14 Dec 1992 16:34:56 MET."
  24874.              <9212141634.aa11558@hermix.markv.com> 
  24875. From: Guido.van.Rossum@cwi.nl
  24876. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24877. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24878. Date: Tue, 15 Dec 1992 12:27:49 +0100
  24879. Sender: Guido.van.Rossum@cwi.nl
  24880.  
  24881. >Has ANYONE compiled a DOS version of Python yet?????
  24882.  
  24883. Yes, you can ftp it from ftp.cwi.nl, directory pub, file python0.9.6.zip.
  24884. I didn't compile it, and can't test it, so you are on your own using
  24885. it though. I have heard that there are some minor problems with it
  24886. (e.g. list.index() returning a wrong value and there may be a problem
  24887. with breaking out of a loop sometimes), but basically it is fully
  24888. functioning.  The "posix" module supports all the Unix functions that
  24889. are reasonably implementable on DOS.
  24890.  
  24891. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24892. "Exploding is a perfectly normal medical phenomenon."
  24893. 
  24894. 
  24895. Replied: Wed, 16 Dec 1992 10:32:49 +0100
  24896. Replied: Lance Ellinghouse <lance@markv.com>
  24897. Replied: python-list@cwi.nl
  24898. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24899.     id AA15949 (5.65b/3.2/CWI-Amsterdam); Wed, 16 Dec 1992 00:47:02 +0100
  24900. From: Lance Ellinghouse <lance@markv.com>
  24901. X-Mailer: SCO System V Mail (version 3.2)
  24902. To: python-list@cwi.nl
  24903. Subject: something simple..
  24904. Date: Tue, 15 Dec 92 15:46:21 PST
  24905. Message-Id:  <9212151546.aa23431@hermix.markv.com>
  24906.  
  24907. I must be missing something here...
  24908. I have a string ( t = '10.4' ) and I want to make
  24909. this into a float type so I can do some addition and such on
  24910. it..
  24911.  
  24912. How do I do this??????
  24913.  
  24914. Thanks,
  24915. Lance Ellinghouse
  24916. lance@markv.com
  24917. 
  24918. 
  24919. Received: from hermix.markv.com by charon.cwi.nl with SMTP
  24920.     id AA01783 (5.65b/3.2/CWI-Amsterdam); Wed, 16 Dec 1992 03:46:49 +0100
  24921. From: Lance Ellinghouse <lance@markv.com>
  24922. X-Mailer: SCO System V Mail (version 3.2)
  24923. To: python-list@cwi.nl
  24924. Subject: Re: DOS version of Python
  24925. Date: Tue, 15 Dec 92 18:46:20 PST
  24926. Message-Id:  <9212151846.aa29299@hermix.markv.com>
  24927.  
  24928. The DOS version on ftp.cwi.nl is corrupted.. don't
  24929. waste your time or bandwidth getting it.. :(
  24930.  
  24931. Lance
  24932. 
  24933. 
  24934. To: Lance Ellinghouse <lance@markv.com>
  24935. cc: python-list@cwi.nl
  24936. Subject: Re: something simple.. 
  24937. In-reply-to: Your message of "Tue, 15 Dec 1992 15:46:21 MET."
  24938.              <9212151546.aa23431@hermix.markv.com> 
  24939. From: Guido.van.Rossum@cwi.nl
  24940. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24941. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24942. Date: Wed, 16 Dec 1992 10:32:47 +0100
  24943. Sender: guido
  24944.  
  24945. >I must be missing something here...
  24946. >I have a string ( t = '10.4' ) and I want to make
  24947. >this into a float type so I can do some addition and such on
  24948. >it..
  24949.  
  24950. Try the built-in function eval()...
  24951.  
  24952. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24953. "He knew how to treat a female impersonator"
  24954. 
  24955. 
  24956. To: Lance Ellinghouse <lance@markv.com>
  24957. Subject: Re: DOS version of Python 
  24958. In-reply-to: Your message of "Wed, 16 Dec 1992 10:12:21 MET."
  24959.              <9212161012.aa20947@hermix.markv.com> 
  24960. From: Guido.van.Rossum@cwi.nl
  24961. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  24962. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  24963. Date: Thu, 17 Dec 1992 11:10:15 +0100
  24964. Sender: guido
  24965.  
  24966. >I used binary twice.. 
  24967. >I tried "pkunzip -t" to test it.. said CRC error...
  24968.  
  24969. OK, I'll see what I can do.  It unpacks here but I have no idea if the
  24970. unix unzip program test the crc at all.
  24971.  
  24972. Sorry,
  24973.  
  24974. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  24975. "I'm afraid we're fresh out of Red Leicester, sir"
  24976. 
  24977. 
  24978. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  24979.     id AA19708 (5.65b/3.3/CWI-Amsterdam); Fri, 18 Dec 1992 18:22:36 +0100
  24980. Received: from elvis.med.virginia.edu by uvaarpa.Virginia.EDU id aa01572;
  24981.           18 Dec 92 12:22 EST
  24982. Received: by elvis.med.Virginia.EDU (5.65c/1.34)
  24983.     id AA19306; Fri, 18 Dec 1992 12:22:32 -0500
  24984. From: "Steven D. Majewski" <sdm7g@elvis.med.Virginia.EDU>
  24985. Message-Id: <199212181722.AA19306@elvis.med.Virginia.EDU>
  24986. Subject: Re: Building Python 0.9.7b for IBM/AIX-6000 with GL support 
  24987. To: python-list@cwi.nl
  24988. Date: Fri, 18 Dec 92 12:22:31 EST
  24989. In-Reply-To: <199212181707.AA20528@elvis.med.Virginia.EDU>; from "Steven D. Majewski" at Dec 18, 92 12:07 pm
  24990. X-In-Reply-To: "myself" <sdm7g@Virginia.EDU>
  24991. X-Mailer: ELM [version 2.3 PL11]
  24992.  
  24993.  
  24994. I suppose the *proper* way around this is to modify cstubs & cgen.py
  24995. to generate the correct glmodule.c for AIX. 
  24996.  
  24997. Any tips on this before I try to decipher the source and figure out
  24998. what it's doing. 
  24999.  
  25000. [ If that is the solution, then this will probably have to wait till
  25001.   January. ] 
  25002.  
  25003. - Steve Majewski
  25004.  
  25005. 
  25006. 
  25007. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  25008.     id AA19713 (5.65b/3.3/CWI-Amsterdam); Fri, 18 Dec 1992 18:23:06 +0100
  25009. Received: by voorn.cwi.nl with SMTP
  25010.     id AA19028 (5.65b/3.2/CWI-Amsterdam); Fri, 18 Dec 1992 18:23:06 +0100
  25011. Message-Id: <9212181723.AA19028.guido@voorn.cwi.nl>
  25012. To: "Steven D. Majewski" <sdm7g@elvis.med.Virginia.EDU>
  25013. Cc: python-list@cwi.nl
  25014. Subject: Re: Building Python 0.9.7b for IBM/AIX-6000 with GL support 
  25015. In-Reply-To: Your message of "Fri, 18 Dec 1992 12:07:16 MET."
  25016.              <199212181707.AA20528@elvis.med.Virginia.EDU> 
  25017. From: Guido.van.Rossum@cwi.nl
  25018. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  25019. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  25020. Date: Fri, 18 Dec 1992 18:23:05 +0100
  25021. Sender: Guido.van.Rossum@cwi.nl
  25022.  
  25023. >I have managed to rebuild Python 0.9.7b successfully on my new IBM RS6000.
  25024. >( I haven't been active on this list lately - I've been busy trying to port
  25025. >  a lot of stuff off of our old VAX-750 onto various PC's, Mac's & unixes. ) 
  25026.  
  25027. [Some stuff deleted -- I'll see if I can make the necessary changes in
  25028. the Makefile to make life easier in this case...]
  25029.  
  25030. >Now, I've been trying to rebuild it with GL support. 
  25031. [...]
  25032. >|.endpupmode
  25033. >|.pupmode
  25034. >|.dglclose
  25035. >|.dglopen
  25036. >|.qgetfd
  25037. [...]
  25038. >dglclose & dglopen are on the list of unsupported functions. 
  25039. >( Meaning, they SHOULD be there for the linker to resolve, but they don't DO
  25040. >anything! ) 
  25041. >
  25042. >endpupmode & popmode are listed on the obsolete list, but they aren't 
  25043. >defined in the glports.h include file.
  25044. >
  25045. >And I can't find qgetfd at all! 
  25046.  
  25047. My suggestion: edit the "cstubs" file, commenting out the lines that
  25048. define these functions.  Then regenerate glmodule.c (Make should do
  25049. this automatically) and then relink.  These functions are all
  25050. non-essential.  See demo/sgi/gl for some examples...
  25051.  
  25052. I'll remove all from the next release, except qgetfd, which is
  25053. essential for some applications, but I may add a note to the
  25054. installation notes.
  25055.  
  25056. (In a hurry,)
  25057.  
  25058. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  25059. "Well I'm a plumber.  I can't act"
  25060. 
  25061. 
  25062. Replied: Mon, 21 Dec 1992 18:35:44 +0100
  25063. Replied: "python-list "
  25064. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  25065.     id AA02891 (5.65b/3.4/CWI-Amsterdam); Mon, 21 Dec 1992 16:53:50 +0100
  25066. Received: from elvis.med.virginia.edu by uvaarpa.Virginia.EDU id aa05646;
  25067.           21 Dec 92 10:53 EST
  25068. Received: by elvis.med.Virginia.EDU (5.65c/1.34)
  25069.     id AA13407; Mon, 21 Dec 1992 10:53:47 -0500
  25070. Date: Mon, 21 Dec 1992 10:46:15 +22306404 (EST)
  25071. From: "Steven D. Majewski" <sdm7g@Virginia.EDU>
  25072. Subject: sockets, fork() & makefile()
  25073. To: python-list@cwi.nl
  25074. In-Reply-To: <199212181722.AA19306@elvis.med.Virginia.EDU>
  25075. Message-Id: <Pine.3.05a.9212211013.A15960-a100000@elvis.med.Virginia.EDU>
  25076. Mime-Version: 1.0
  25077. Content-Type: TEXT/PLAIN; charset=US-ASCII
  25078.  
  25079.  
  25080. I'm trying to open a socket, fork(), dup a file descriptor with makefile(),
  25081. assign  to sys.stdin and exec() another procedure. 
  25082.  
  25083. And it's not working quite right. 
  25084. Should it? 
  25085.  
  25086. The assignment to stdin and exec is the bit that I am suspicious of - will
  25087. python actually export that assignment ? 
  25088.  
  25089. [ In the mean time, I'll go back and check over the code again - maybe I'm 
  25090.   closing something that shouldn't have been closed... ] 
  25091.  
  25092. ===============================================================================
  25093.  Steven D. Majewski            University of Virginia 
  25094.  sdm7g@Virginia.EDU            Box 449 Health Sciences Center
  25095.  Voice: (804)-982-0831            1300 Jefferson Park Avenue
  25096.  FAX:   (804)-982-1616            Charlottesville, VA 22908
  25097. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25098. Former UVA Department of Physiology, now Department of Molecular Physiology
  25099. and Biological Physics! [ Still the same spacious offices in Jordan Hall 
  25100. - only the letterhead has changed! ]
  25101.  
  25102. 
  25103. 
  25104. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  25105.     id AA02891 (5.65b/3.4/CWI-Amsterdam); Mon, 21 Dec 1992 16:53:50 +0100
  25106. Received: from elvis.med.virginia.edu by uvaarpa.Virginia.EDU id aa05646;
  25107.           21 Dec 92 10:53 EST
  25108. Received: by elvis.med.Virginia.EDU (5.65c/1.34)
  25109.     id AA13407; Mon, 21 Dec 1992 10:53:47 -0500
  25110. Date: Mon, 21 Dec 1992 10:46:15 +22306404 (EST)
  25111. From: "Steven D. Majewski" <sdm7g@Virginia.EDU>
  25112. Subject: sockets, fork() & makefile()
  25113. To: python-list@cwi.nl
  25114. In-Reply-To: <199212181722.AA19306@elvis.med.Virginia.EDU>
  25115. Message-Id: <Pine.3.05a.9212211013.A15960-a100000@elvis.med.Virginia.EDU>
  25116. Mime-Version: 1.0
  25117. Content-Type: TEXT/PLAIN; charset=US-ASCII
  25118.  
  25119.  
  25120. I'm trying to open a socket, fork(), dup a file descriptor with makefile(),
  25121. assign  to sys.stdin and exec() another procedure. 
  25122.  
  25123. And it's not working quite right. 
  25124. Should it? 
  25125.  
  25126. The assignment to stdin and exec is the bit that I am suspicious of - will
  25127. python actually export that assignment ? 
  25128.  
  25129. [ In the mean time, I'll go back and check over the code again - maybe I'm 
  25130.   closing something that shouldn't have been closed... ] 
  25131.  
  25132. ===============================================================================
  25133.  Steven D. Majewski            University of Virginia 
  25134.  sdm7g@Virginia.EDU            Box 449 Health Sciences Center
  25135.  Voice: (804)-982-0831            1300 Jefferson Park Avenue
  25136.  FAX:   (804)-982-1616            Charlottesville, VA 22908
  25137. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25138. Former UVA Department of Physiology, now Department of Molecular Physiology
  25139. and Biological Physics! [ Still the same spacious offices in Jordan Hall 
  25140. - only the letterhead has changed! ]
  25141.  
  25142. 
  25143. 
  25144. Replied: Mon, 21 Dec 1992 22:22:07 +0100
  25145. Replied: "python-list@cwi.nl "
  25146. Received: from meerman.cwi.nl by charon.cwi.nl with SMTP
  25147.     id AA00637 (5.65b/3.4/CWI-Amsterdam); Mon, 21 Dec 1992 20:25:26 +0100
  25148. Received: from uvaarpa.Virginia.EDU by meermin.cwi.nl with SMTP
  25149.     id AA22693 (5.65b/3.2/CWI-Amsterdam); Mon, 21 Dec 1992 17:45:22 +0100
  25150. Received: from elvis.med.virginia.edu by uvaarpa.Virginia.EDU id aa09885;
  25151.           21 Dec 92 11:44 EST
  25152. Received: by elvis.med.Virginia.EDU (5.65c/1.34)
  25153.     id AA09651; Mon, 21 Dec 1992 11:44:19 -0500
  25154. Date: Mon, 21 Dec 1992 11:39:21 +22306404 (EST)
  25155. From: "Steven D. Majewski" <sdm7g@Virginia.EDU>
  25156. Subject: Re: Building Python 0.9.7b for IBM/AIX-6000 with GL support 
  25157. To: Guido.van.Rossum@cwi.nl
  25158. Cc: "Steven D. Majewski" <sdm7g@elvis.med.Virginia.EDU>, python-list@cwi.nl
  25159. In-Reply-To: <9212181723.AA19028.guido@voorn.cwi.nl>
  25160. Message-Id: <Pine.3.05a.9212211116.A2980-c100000@elvis.med.Virginia.EDU>
  25161. Mime-Version: 1.0
  25162. Content-Type: TEXT/PLAIN; charset=US-ASCII
  25163.  
  25164.  
  25165.  
  25166. Thanks Guido, it works. 
  25167. For anyone else who wants it ( till that time that you have the time
  25168. to fold these changes into Configure.py ) here are instructions.
  25169.  
  25170. To Build python 0.9.7b for IBM/rs6000 AIX with GL library support:
  25171.  
  25172.  
  25173. In "Makefile", Uncomment the GL option, except replace line:
  25174.  
  25175. > #GL_LIBS=    -lgl_s
  25176.  
  25177. with:
  25178.  
  25179. > GL_LIBS=    -lgl            # for AIX
  25180.  
  25181.  
  25182. GL for AIX does not include a number of "obsolete" GL functions.
  25183. Some of these have stubs in "/usr/lpp/GL/examples/glport.h" 
  25184. ( See the comments in that file and the info entry for GL compatibility 
  25185.   for further notes. ) 
  25186.  
  25187. In "cstubs", insert the following lines among the other #include lines:
  25188.  
  25189. > #define    _GL_PORT_C_    1            /* (sdm7g) for AIX */
  25190. > #include "/usr/lpp/GL/examples/glport.h"        /* (sdm7g) for AIX */
  25191.  
  25192. This will not remove ALL of the unresolved names. You must also comment out
  25193. the following function definitions in cstubs:
  25194.  
  25195. > ### void    endpupmode              # not in aix GL (sdm7g)
  25196. > ### void    pupmode                # not in aix GL (sdm7g)
  25197.  
  25198. > ### void dglclose        long s        # not in aix GL (sdm7g)
  25199. > ### long dglopen        char *s long s    # not in aix GL (sdm7g)
  25200.  
  25201. > ### long qgetfd        # but not in aix GL  (sdm7g)
  25202.  
  25203.  
  25204. The last one is apparenlty, not an old obsolete GL function, but a new one
  25205. which is not included in IBM's AIX GL. 
  25206.  
  25207.  
  25208. Most of the python/demo/sgi/gl  programs appear to work except for nurbs,
  25209. which gives this error message:
  25210.  
  25211. > $ python nurbs.py
  25212. > MemoryError
  25213. > Stack backtrace (innermost last):
  25214. >   File "nurbs.py", line 171
  25215. >     main()
  25216. >   File "nurbs.py", line 77
  25217. >     make_lights()
  25218. >   File "nurbs.py", line 152
  25219. >     lmdef(DEFLMODEL,1,[])
  25220.  
  25221. There may be a few other hidden traps, since some of the code might assume that
  25222. if you have GL, you are running a SGI, and therefore may have other sgi libraries
  25223. and functions.
  25224.  
  25225.  
  25226. ! Next project - maybe I can figure out how to write an AIX audio module. 
  25227. ! but that may have to wait till after adding sockets to Mac/Dos python,
  25228. ! which is something I actually NEED! 
  25229.  
  25230. ===============================================================================
  25231.  Steven D. Majewski            University of Virginia 
  25232.  sdm7g@Virginia.EDU            Box 449 Health Sciences Center
  25233.  Voice: (804)-982-0831            1300 Jefferson Park Avenue
  25234.  FAX:   (804)-982-1616            Charlottesville, VA 22908
  25235. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25236. Former UVA Department of Physiology, now Department of Molecular Physiology
  25237. and Biological Physics! [ Still the same spacious offices in Jordan Hall 
  25238. - only the letterhead has changed! ]
  25239.  
  25240. 
  25241. 
  25242. Received: from uvaarpa.Virginia.EDU by charon.cwi.nl with SMTP
  25243.     id AA01108 (5.65b/3.4/CWI-Amsterdam); Mon, 21 Dec 1992 21:03:43 +0100
  25244. Received: from elvis.med.virginia.edu by uvaarpa.Virginia.EDU id aa24926;
  25245.           21 Dec 92 15:03 EST
  25246. Received: by elvis.med.Virginia.EDU (5.65c/1.34)
  25247.     id AA11195; Mon, 21 Dec 1992 15:03:24 -0500
  25248. Date: Mon, 21 Dec 1992 14:46:43 +22306404 (EST)
  25249. From: "Steven D. Majewski" <sdm7g@Virginia.EDU>
  25250. Subject: Re: sockets, fork() & makefile() 
  25251. To: Guido.van.Rossum@cwi.nl
  25252. Cc: python-list@cwi.nl
  25253. In-Reply-To: <9212211735.AA00857@voorn.cwi.nl>
  25254. Message-Id: <Pine.3.05a.9212211440.A2986-b100000@elvis.med.Virginia.EDU>
  25255. Mime-Version: 1.0
  25256. Content-Type: TEXT/PLAIN; charset=US-ASCII
  25257.  
  25258.  
  25259. I wrote:
  25260. sdm7g>The assignment to stdin and exec is the bit that I am suspicious of - will
  25261. sdm7g>python actually export that assignment ? 
  25262.  
  25263. On Mon, 21 Dec 1992 Guido.van.Rossum@cwi.nl wrote:
  25264.  
  25265. > No, I'm sorry, it doesn't.  I suggest you use popen() and construct a
  25266. > piece of shell syntax to redirect the socket's file number to 0,
  25267. > something like
  25268. >     sock = socket(...) # Create a socket
  25269. >     cmd = 'some_program arg1 arg2 ...' # A command and its arguments
  25270. >     fd = sock.fileno() # Get the socket's file number as an integer
  25271. >     cmd = cmd + ' 0<&' + `fd` # Append "0&<N" where N is the file number
  25272. >     pipe = os.popen(cmd, 'w') # Fork+exec the program; it ignores its stdin
  25273. >     pipe.close() # Write an EOF on the pipe
  25274.  
  25275. Thanks. 
  25276. socket.fileno() is not in my copy of the library reference, but I *do* see
  25277. it there with  "mysocket.__methods__" . 
  25278. Your example, above, should be sufficient - I just want a python script that 
  25279. will read in an initial command from a socket and dispatch to another 
  25280. program, with input and output reassigned to the socket. 
  25281.  
  25282.  
  25283. ===============================================================================
  25284.  Steven D. Majewski            University of Virginia 
  25285.  sdm7g@Virginia.EDU            Box 449 Health Sciences Center
  25286.  Voice: (804)-982-0831            1300 Jefferson Park Avenue
  25287.  FAX:   (804)-982-1616            Charlottesville, VA 22908
  25288. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25289. Former UVA Department of Physiology, now Department of Molecular Physiology
  25290. and Biological Physics! [ Still the same spacious offices in Jordan Hall 
  25291. - only the letterhead has changed! ]
  25292.  
  25293. 
  25294. 
  25295. Received: from meerman.cwi.nl by charon.cwi.nl with SMTP
  25296.     id AA00532 (5.65b/3.4/CWI-Amsterdam); Mon, 21 Dec 1992 20:25:07 +0100
  25297. Received: from voorn.cwi.nl by meermin.cwi.nl with SMTP
  25298.     id AA23038 (5.65b/3.2/CWI-Amsterdam); Mon, 21 Dec 1992 18:37:02 +0100
  25299. Received: by voorn.cwi.nl with SMTP; Mon, 21 Dec 1992 17:35:47 GMT
  25300. Message-Id: <9212211735.AA00857@voorn.cwi.nl>
  25301. To: python-list@cwi.nl
  25302. Subject: Re: sockets, fork() & makefile() 
  25303. In-Reply-To: Your message of "Mon, 21 Dec 1992 10:46:15."
  25304.              <Pine.3.05a.9212211013.A15960-a100000@elvis.med.Virginia.EDU> 
  25305. From: Guido.van.Rossum@cwi.nl
  25306. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  25307. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  25308. Date: Mon, 21 Dec 1992 18:35:44 +0100
  25309. Sender: Guido.van.Rossum@cwi.nl
  25310.  
  25311. >I'm trying to open a socket, fork(), dup a file descriptor with makefile(),
  25312. >assign  to sys.stdin and exec() another procedure. 
  25313. >
  25314. >And it's not working quite right. 
  25315. >Should it? 
  25316. >
  25317. >The assignment to stdin and exec is the bit that I am suspicious of - will
  25318. >python actually export that assignment ? 
  25319.  
  25320. No, I'm sorry, it doesn't.  I suggest you use popen() and construct a
  25321. piece of shell syntax to redirect the socket's file number to 0,
  25322. something like
  25323.  
  25324.     sock = socket(...) # Create a socket
  25325.     cmd = 'some_program arg1 arg2 ...' # A command and its arguments
  25326.     fd = sock.fileno() # Get the socket's file number as an integer
  25327.     cmd = cmd + ' 0<&' + `fd` # Append "0&<N" where N is the file number
  25328.     pipe = os.popen(cmd, 'w') # Fork+exec the program; it ignores its stdin
  25329.     pipe.close() # Write an EOF on the pipe
  25330.  
  25331.  
  25332. I could hack something in exec() to dup the file descriptors from
  25333. sys.std{in,out,err} back to 0, 1, 2, except that (in a yet unreleased
  25334. version) it is possible to replace any file with a class instance, as
  25335. long as the instance has a write() method or read() and readline()
  25336. methods.  In this case, what should I use for the file descriptor in
  25337. the exec'ed process?
  25338.  
  25339.  
  25340. Maybe it's better to provide low-level operations posix.dup(i),
  25341. posix.open(...), posix.close(i) etc. and an fdopen()-like interface,
  25342. so that if necessary you can do anything you like yourself (some
  25343. programs may want special files open as fd 5...).  This, however, has
  25344. the disadvantage that uninformed use can cause unexplainable problems,
  25345. e.g. what if a novice user accidentally closes fd 1...
  25346.  
  25347.  
  25348. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  25349. "Look matey, this parrot wouldn't voom if I put four thousand volts
  25350. through it"
  25351. 
  25352. 
  25353. Received: from voorn.cwi.nl by charon.cwi.nl with SMTP
  25354.     id AA03565 (5.65b/3.4/CWI-Amsterdam); Mon, 21 Dec 1992 22:22:08 +0100
  25355. Received: by voorn.cwi.nl with SMTP
  25356.     id AA00584 (5.65b/3.2/CWI-Amsterdam); Mon, 21 Dec 1992 22:22:08 +0100
  25357. Message-Id: <9212212122.AA00584.guido@voorn.cwi.nl>
  25358. To: python-list@cwi.nl
  25359. Subject: Re: Building Python 0.9.7b for IBM/AIX-6000 with GL support 
  25360. In-Reply-To: Your message of "Mon, 21 Dec 1992 11:39:21."
  25361.              <Pine.3.05a.9212211116.A2980-c100000@elvis.med.Virginia.EDU> 
  25362. From: Guido.van.Rossum@cwi.nl
  25363. X-Organization: CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands
  25364. X-Phone: +31 20 5924127 (work), +31 20 6225521 (home), +31 20 5924199 (fax)
  25365. Date: Mon, 21 Dec 1992 22:22:07 +0100
  25366. Sender: Guido.van.Rossum@cwi.nl
  25367.  
  25368. >Most of the python/demo/sgi/gl  programs appear to work except for nurbs,
  25369. >which gives this error message:
  25370. >
  25371. >> $ python nurbs.py
  25372. >> MemoryError
  25373. >> Stack backtrace (innermost last):
  25374. >>   File "nurbs.py", line 171
  25375. >>     main()
  25376. >>   File "nurbs.py", line 77
  25377. >>     make_lights()
  25378. >>   File "nurbs.py", line 152
  25379. >>     lmdef(DEFLMODEL,1,[])
  25380.  
  25381. By sheer coincidence I ran into this myself the other day.  God knows
  25382. how long it has been there waiting until I tried those old demos
  25383. again!
  25384.  
  25385. The cause is simple: the empty list [] is being copied to a malloc'ed
  25386. array of floats which causes a call to malloc(0). This returns NULL on
  25387. some systems, and of course the check for malloc errors takes that as
  25388. an error return.  This can happen in other situations too.
  25389.  
  25390. As a temporary measure (though this is a waste of some memory), I
  25391. added 1 to the malloc and realloc arguments in the definitions of NEW
  25392. and RENEW in [my]malloc.h (the file's name changed at I don't know
  25393. which release):
  25394.  
  25395. /* XXX Always allocate one extra byte, since some malloc's return NULL
  25396.    XXX for malloc(0) or realloc(p, 0). */
  25397. #define NEW(type, n) ( (type *) malloc(1 + (n) * sizeof(type)) )
  25398. #define RESIZE(p, type, n) \
  25399.     if ((p) == NULL) \
  25400.         (p) =  (type *) malloc(1 + (n) * sizeof(type)); \
  25401.     else \
  25402.         (p) = (type *) realloc((ANY *)(p), 1 + (n) * sizeof(type))
  25403.  
  25404. Eventually I'll probably use a less wasteful fix (Python is a really
  25405. heavy malloc user and every byte counts!).
  25406.  
  25407. I wonder -- will I really have to use a configure script like the GNU
  25408. software uses?  I'd much rather write portable code that doesn't need
  25409. to be configured, but I hate it if it gets wasteful that way...
  25410.  
  25411. --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
  25412. "The man from the cat-detector van."
  25413. 
  25414. 
  25415. Received: from relay2.UU.NET by charon.cwi.nl with SMTP
  25416.     id AA11065 (5.65b/3.5/CWI-Amsterdam); Wed, 23 Dec 1992 16:02:22 +0100
  25417. Received: from uunet.uu.net (via LOCALHOST.UU.NET) by relay2.UU.NET with SMTP 
  25418.     (5.61/UUNET-internet-primary) id AA12685; Wed, 23 Dec 92 10:02:13 -0500
  25419. Received: from astro.UUCP by uunet.uu.net with UUCP/RMAIL
  25420.     (queueing-rmail) id 100149.15581; Wed, 23 Dec 1992 10:01:49 EST
  25421. Received: by astro.sunrise.com (/\==/\ Smail3.1.22.1 #22.3)
  25422.     id <m0n4WaN-00041TC@astro.sunrise.com>; Wed, 23 Dec 92 08:57 EST
  25423. Message-Id: <m0n4WaN-00041TC@astro.sunrise.com>
  25424. From: drew@sunrise.com (Drew Jenkins)
  25425. Subject: Subscribe
  25426. To: python-list-request@cwi.nl
  25427. Date: Wed, 23 Dec 92 8:57:17 EST
  25428. X-Mailer: ELM [version 2.3 PL11]
  25429.  
  25430. Guido,
  25431.  
  25432. Please add me to the python mailing list.
  25433.  
  25434. Thanks,
  25435.  
  25436. Drew Jenkins            "Attitude is Everything"
  25437. drew@sunrise.com                 -- Bingen Bart
  25438. 
  25439.